python 爬取學信網(wǎng)登錄頁面的例子
更新時間:2019年08月13日 15:04:11 作者:宇風-飛揚
今天小編就為大家分享一篇python 爬取學信網(wǎng)登錄頁面的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
我們以學信網(wǎng)為例爬取個人信息
**如果看不清楚
按照以下步驟:**
1.火狐為例 打開需要登錄的網(wǎng)頁–> F12 開發(fā)者模式 (鼠標右擊,點擊檢查元素)–點擊網(wǎng)絡 –>需要登錄的頁面登錄下–> 點擊網(wǎng)絡找到 一個POST提交的鏈接點擊–>找到post(注意該post中信息就是我們提交時需要構(gòu)造的表單信息)

import requests
from bs4 import BeautifulSoup
from http import cookies
import urllib
import http.cookiejar
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0',
'Referer':'https://account.chsi.com.cn/passport/login?service=https://my.chsi.com.cn/archive/j_spring_cas_security_check',
}
session = requests.Session()
session.headers.update(headers)
username = 'xxx'
password = 'xxx'
url = 'https://account.chsi.com.cn/passport/login?service=https://my.chsi.com.cn/archive/j_spring_cas_security_check'
def login(username,password,lt,_eventId='submit'): #模擬登入函數(shù)
#構(gòu)造表單數(shù)據(jù)
data = { #需要傳去的數(shù)據(jù)
'_eventId':_eventId,
'lt':lt,
'password':password,
'submit':u'登錄',
'username':username,
}
html = session.post(url,data=data,headers=headers)
def get_lt(url): #解析登入界面_eventId
html = session.get(url)
#獲取 lt
soup = BeautifulSoup(html.text,'lxml',from_encoding="utf-8")
lt=soup.find('input',type="hidden")['value']
return lt
lt = get_lt(url)#獲取登錄form表單信息 以學信網(wǎng)為例
login(username,password,lt)
login_url = 'https://my.chsi.com.cn/archive/gdjy/xj/show.action'
per_html = session.get(login_url)
soup = BeautifulSoup(per_html.text,'lxml',from_encoding="utf-8")
print(soup)
for tag in soup.find_all('table',class_='mb-table'):
print(tag)
for tag1 in tag.find_all('td'):
title= tag1.get_text();
print(title)
以上這篇python 爬取學信網(wǎng)登錄頁面的例子就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python+SeaTable實現(xiàn)計算兩個日期間的工作日天數(shù)
在實際的項目管理、任務管理、工作計劃等場景中,某些時間段會涉及雙休日、法定節(jié)假日,甚至還有公司自定義的工作時間安排,所以就需要計算出兩個日期間的實際工作日天數(shù)。本文用Python+SeaTable實現(xiàn)這一需求,需要的可以參考一下2022-07-07
Python實現(xiàn)單鏈表中元素的反轉(zhuǎn)
這篇文章主要為大家詳細介紹了Python實現(xiàn)單鏈表中元素的反轉(zhuǎn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05

