溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python通過偽裝頭部數(shù)據(jù)抵抗反爬蟲的實例

發(fā)布時間:2020-09-18 10:13:38 來源:腳本之家 閱讀:178 作者:JackReach 欄目:開發(fā)技術(shù)

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ù)偽裝思路

通過http向服務(wù)器提交數(shù)據(jù),以下是通過Fiddler 抓取python沒有偽裝的報文頭信息

GET /u012870721 HTTP/1.1
Accept-Encoding: identity
Host: blog.csdn.net
User-Agent: <span >Python-urllib/3.6</span>
Connection: close

Python-urllib/3.6

很明顯啊,我們暴露了?,F(xiàn)在要問了,該怎么!模擬瀏覽器,讓自己偽裝成瀏覽器,一下是瀏覽器訪問發(fā)送的頭部數(shù)據(jù)

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
Referer: http://write.blog.csdn.net/postlist
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.8

0x02代碼實現(xiàn)

from urllib import request
html_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;
# }

偽裝后進行發(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通過偽裝頭部數(shù)據(jù)抵抗反爬蟲的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI