网页下载器urllib2第二种方法出错
# coding:utf-8
import urllib2
url="http://www.baidu.com"
print "第二种方法"
request=urllib2.urlopen(url)
request.add_header("user-agent","Mozilla/5.0")
response2=urllib2.urlopen(url)
print response2.getcode()
print len(response2.read())
运行结果
第二种方法
Traceback (most recent call last):
File "D:\svn\imooc\test\test_urlib2.py", line 10, in <module>
request.add_header("user-agent","Mozilla/5.0")
AttributeError: addinfourl instance has no attribute 'add_header'