0x00 環(huán)境
系統(tǒng)環(huán)境:win10
編寫工具:JetBrains PyCharm Community Edition 2017.1.2 x64
python 版本:python-3.6.2
抓包工具:Fiddler 4
0x01 頭部數(shù)據(jù)偽裝思路
通過(guò)http向服務(wù)器提交數(shù)據(jù),以下是通過(guò)Fiddler 抓取python沒(méi)有偽裝的報(bào)文頭信息
GET /u012870721 HTTP/1.1Accept-Encoding: identityHost: blog.csdn.netUser-Agent: <span style="color:#ff0000;">Python-urllib/3.6</span>Connection: close
Python-urllib/3.6
很明顯啊,我們暴露了?,F(xiàn)在要問(wèn)了,該怎么!模擬瀏覽器,讓自己偽裝成瀏覽器,一下是瀏覽器訪問(wèn)發(fā)送的頭部數(shù)據(jù)
Connection: keep-aliveUpgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36Referer: http://write.blog.csdn.net/postlistAccept-Encoding: gzip, deflateAccept-Language: zh-CN,zh;q=0.8
0x02代碼實(shí)現(xiàn)
from urllib import requesthtml_url = "http://blog.csdn.net/u012870721";#偽裝構(gòu)造頭header ={ "Connection": "keep-alive", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36", "Accept":" text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept-Encoding": "gzip,deflate", "Accept-Language": "zh-CN,zh;q=0.8"};#int main()#{req = request.Request(url=html_url, headers=header);resp = request.urlopen(req);# return 0;# }偽裝后進(jìn)行發(fā)送的信息頭
GET /u012870721 HTTP/1.1 Host: blog.csdn.net Connection: close Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 Accept-Encoding: gzip,deflate Accept-Language: zh-CN,zh;q=0.8
以上這篇python通過(guò)偽裝頭部數(shù)據(jù)抵抗反爬蟲的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選