系统运维
在学习爬虫的过程中在有的时候没使用头在使用python的爬虫脚本刚爬了两次,就只是测试了一下就打不开这个网页了,刚开始还一直迷糊着,到后来才知道,python在做爬虫的时候默认的user agent就是python的大版本,python2.7.的user-agent: python-urllib/2.7;python3.5.的user-agent: python-urllib/3.5
下面来做个试验:
python代码如下:
python2
import urllib2url = http://www.baidu.com/request = urllib2.request(url)response = urllib2.urlopen(request)print(response.read())python3
from urllib import requesturl = http://www.baidu.com/req = request.request(url)response = request.urlopen(req)print(response.read().decode()我们开启fiddler,运行完成代码,然后在fiddler上面查看下我们的数据
很明显就是python的版本,
因此我们在学习爬虫的时候无论爬什么,代码最好都要加上这个头信息
下面我们在代码上加入一个头
from urllib import requestheaders = { user-agent:mozilla/5.0 (windows nt 10.0; wow64}url = http://www.baidu.com/req = request.request(url,headers=headers)response = request.urlopen(req)print(response.read().decode())抓包的结果如下:
163企业阿里云服务器ecs忘记续费了网站优化中流量突然大幅下降的原因打不开了麻烦处理一下-其他问题域名申请信息有什么重要性?域名注册注意什么?自学编程从哪学起CC攻击和ddos的区别云主机如何开机