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

不言
发布: 2018-05-07 14:18:37
原创
1645 人浏览过

这篇文章主要介绍了关于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
登录后复制

相关推荐:

一些Centos Python 生产环境的部署命令

以上是python通过伪装头部数据抵抗反爬虫的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!