urllib2.HTTPError: HTTP Error 403: Forbidden 错误

转自:http://www.douban.com/note/131370224/

出现urllib2.HTTPError: HTTP Error 403: Forbidden错误是由于网站禁止爬虫,可以在请求加上头信息,伪装成浏览器访问

#伪装浏览器头
headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}

req = urllib2.Request(url = 'http://topic.csdn.net/u/20110123/15/F71C5EBB-7704-480B-9379-17A96E920FEE.html',headers = headers)

feeddata = urllib2.urlopen(req).read()

#或者
#opener = urllib2.build_opener()

#feeddata = opener.open(request).read()

print feeddata.decode('u8')

你可能感兴趣的:(python,error)