利用python爬虫技术本福特_Python爬虫技术(一)--模拟登陆
好了,最后上完整代碼,當當當當~
# -*- coding: utf-8 -*-
import requests
import sys
import urllib2
import re
if __name__ == "__main__":
## 這段代碼是用于解決中文報錯的問題
reload(sys)
sys.setdefaultencoding("utf8")
posturl = "http://ids.chd.edu.cn/authserver/login?service=http://portal.chd.edu.cn/index.portal"
#保存cookies,不保存cookie很危險,登陸成功后不保存cookie服務器將不知道你已經登錄
#或者說服務器不知道你是你,就導致獲得頁面失敗
s = requests.session()
circle = s.get(posturl).text
#查找lt字符串
#長大信息門戶中有幾個隱藏表單項,lt表單項為一個隨機字符串
#其余幾個均為固定字符串
#所以我們必須先得到lt字符串
ltString = '
ltAnswer = re.findall(ltString, circle)
lt = ltAnswer[0].replace('
#構造頭部信息
head = {
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding' : 'gzip,deflate',
'Accept-Language' : 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Host' : 'ids.chd.edu.cn',
'Connection' : 'keep-alive',
#反爬蟲技術,這個說明我們是從這個網頁進入的
'Referer' : 'http://ids.chd.edu.cn/authserver/login?service=http://portal.chd.edu.cn/index.portal',
'Upgrade-Insecure-Requests' : '1',
#偽裝瀏覽器
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0'
}
#構造Post數據
postData = {'_eventId' : "submit",
'btn1' : "",
'dllt' : "userNamePasswordLogin",
'execution': "e1s1",
'lt' : lt,
'password' : "*******",
'rmShown' : "1",
'username' : "123456789",
}
loginhtml = s.post(posturl,data=postData,headers=head)
url2 = 'http://portal.chd.edu.cn'
head2 = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0',
'Referer' : 'http://ids.chd.edu.cn/authserver/login?service=http://portal.chd.edu.cn/index.portal'}
scorehtml = s.get(url2,headers=head2)
print scorehtml.text.decode('gbk','ignore')
好了,本次實驗到此結束,歡迎一起學習更多知識!
總結
以上是生活随笔為你收集整理的利用python爬虫技术本福特_Python爬虫技术(一)--模拟登陆的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python多态_python 多态
- 下一篇: python数字求和输入完第一个数没反应