這篇文章主要介紹了關於python透過偽裝頭部資料抵抗反爬蟲,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
0x00 環境
系統環境:win10
寫工具:JetBrains PyCharm Community Edition 2017.1.2 x64
python 版本:python-3.6.2
抓包工具:Fiddler 4
0x01 頭部資料偽裝想法
透過http向伺服器提交資料,以下是透過Fiddler 抓取python沒有偽裝的報文頭資訊
GET /u012870721 HTTP/1.1 Accept-Encoding: identity Host: blog.csdn.net User-Agent: <span style="color:#ff0000;">Python-urllib/3.6</span> Connection: close
Python-urllib/3.6
很明顯啊,我們暴露了。現在要問了,該怎麼!模擬瀏覽器,讓自己偽裝成瀏覽器,一下是瀏覽器存取發送的頭部資料
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程式碼實作
from urllib import request html_url = "http://blog.csdn.net/u012870721"; #伪装构造头 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; # }
偽裝後進行發送的訊息頭
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透過偽裝頭部資料抵抗反爬蟲的詳細內容。更多資訊請關注PHP中文網其他相關文章!