python通過偽裝頭部資料抵抗反爬蟲

來源:互聯網
上載者:User
這篇文章主要介紹了關於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.1Accept-Encoding: identityHost: blog.csdn.netUser-Agent: <span style="color:#ff0000;">Python-urllib/3.6</span>Connection: close

Python-urllib/3.6

很明顯啊,我們暴露了。現在要問了,該怎麼!類比瀏覽器,讓自己偽裝成瀏覽器,一下是瀏覽器訪問發送的頭部資料

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代碼實現

from urllib import requesthtml_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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.