您好,登錄后才能下訂單哦!
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 >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通過偽裝頭部數據抵抗反爬蟲的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。