这篇文章主要介绍了关于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通过伪装头部数据抵抗反爬虫的详细内容。