用python零基础写爬虫--编写第一个网络爬虫 -2 设置用户代理

1.设置用户代理

默认情况下,urliib2使用python-urllib、2.7 作为用户代理下载网页内容,其中2.7是python的版本号。为避免一些网站禁封这个默认的用户代理,确保下载更加可靠,我们需要控制用户代理的设定。下面代码对download函数设定了一个名称为 “wswp” 的用户代理。

import urllib2
def download(url,user_agent='wswp', num_retries=2):
print 'downloading:',url
headers={'User-agent':user_agent}
request=urllib2.Request(url,headers=headers)
try:
html=urllib2.urlopen(url).read()
except urllib2.URLError as e:
print 'download error:', e.reason
html=None
if num_retries>0:
if hasattr(e, 'code') and 500<=e.code<600:
#recursively retry 5XX http errors
return download(url, user_agent,num_retries-1)
return html


原文地址:https://www.cnblogs.com/mrruning/p/7637441.html