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


本文摘自php中文网,作者不言,侵删。

这篇文章主要介绍了关于python通过伪装头部数据抵抗反爬虫,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

0x00 环境

系统环境:win10

编写工具:JetBrains PyCharm Community Edition 2017.1.2 x64

python 版本:python-3.6.2

抓包工具:Fiddler 4

0x01 头部数据伪装思路

通过http向服务器提交数据,以下是通过Fiddler 抓取python没有伪装的报文头信息

1

2

3

4

5

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

很明显啊,我们暴露了。现在要问了,该怎么!模拟浏览器,让自己伪装成浏览器,一下是浏览器访问发送的头部数据

1

2

3

4

5

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代码实现

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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;

# }

伪装后进行发送的信息头

1

2

3

4

5

6

7

8

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通过伪装头部数据抵抗反爬虫的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python爬虫买什么书?

Python编程如何求2000到2500闰年?

使用Python监控linux内存并将其写入mongodb

Python中的特殊变量__name__有什么用?

Python教程之select模块介绍

Python os.access()方法是什么?os.access有什么作用?

Python怎么安装matplotlib

如何查看Python源代码

Python统计字符个数

Python怎么把字符串变成数字

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...