python通过伪装头部数据抵抗反爬虫的实例

yipeiwu_com5年前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通过伪装头部数据抵抗反爬虫的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python爬虫:通过关键字爬取百度图片

Python爬虫:通过关键字爬取百度图片

使用工具:Python2.7 点我下载 scrapy框架 sublime text3 一。搭建python(Windows版本)  1.安装python2.7 ---然后在cm...

Python3爬虫爬取英雄联盟高清桌面壁纸功能示例【基于Scrapy框架】

Python3爬虫爬取英雄联盟高清桌面壁纸功能示例【基于Scrapy框架】

本文实例讲述了Python3爬虫爬取英雄联盟高清桌面壁纸功能。分享给大家供大家参考,具体如下: 使用Scrapy爬虫抓取英雄联盟高清桌面壁纸 源码地址:https://github.co...

Python爬虫爬取Bilibili弹幕过程解析

Python爬虫爬取Bilibili弹幕过程解析

先来思考一个问题,B站一个视频的弹幕最多会有多少? 比较多的会有2000条吧,这么多数据,B站肯定是不会直接把弹幕和这个视频绑在一起的。 也就是说,有一个视频地址为https://www...

python 网络爬虫初级实现代码

首先,我们来看一个Python抓取网页的库:urllib或urllib2。 那么urllib与urllib2有什么区别呢? 可以把urllib2当作urllib的扩增,比较明显的优势是u...

python爬取盘搜的有效链接实现代码

python爬取盘搜的有效链接实现代码

因为盘搜搜索出来的链接有很多已经失效了,影响找数据的效率,因此想到了用爬虫来过滤出有效的链接,顺便练练手~ 这是本次爬取的目标网址http://www.pansou.com/,首先先搜索...