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爬取B站千万级数据

使用python爬取B站千万级数据

Python(发音:英[?pa?θ?n],美[?pa?θɑ:n]),是一种面向对象、直译式电脑编程语言,也是一种功能强大的通用型语言,已经具有近二...

Python爬虫工程师面试问题总结

注:答案一般在网上都能够找到。 1.对if __name__ == 'main'的理解陈述 2.python是如何进行内存管理的? 3.请写出一段Python代码实现删除一个lis...

python爬虫 基于requests模块的get请求实现详解

需求:爬取搜狗首页的页面数据 import requests # 1.指定url url = 'https://www.sogou.com/' # 2.发起get请求:get方法会返...

Python爬虫DOTA排行榜爬取实例(分享)

Python爬虫DOTA排行榜爬取实例(分享)

1、分析网站 打开开发者工具,我们观察到排行榜的数据并没有在doc里   doc文档 在Javascript里我么可以看到下面代码: ajax的post方法异步请求数据 在...

Python爬虫利用cookie实现模拟登陆实例详解

Python爬虫利用cookie实现模拟登陆实例详解

Cookie,指某些网站为了辨别用户身份、进行session跟踪而储存在用户本地终端上的数据(通常经过加密)。 举个例子,某些网站是需要登录后才能得到你想要的信息的,不登陆只能是游客模式...