我們以學信網為例爬取個人信息
**如果看不清楚
按照以下步驟:**
1.火狐為例 打開需要登錄的網頁?C> F12 開發者模式 (鼠標右擊,點擊檢查元素)?C點擊網絡 ?C>需要登錄的頁面登錄下?C> 點擊網絡找到 一個POST提交的鏈接點擊?C>找到post(注意該post中信息就是我們提交時需要構造的表單信息)
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'): #模擬登入函數 #構造表單數據 data = { #需要傳去的數據 '_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表單信息 以學信網為例 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 爬取學信網登錄頁面的例子就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
