这是最后输出的结果,请教怎么解决啊?
第一种方法
200
118011
第二种方法
Traceback (most recent call last):
File "C:\Users\admin\eclipse-workspace\imoocc\src\test\test1.py", line 16, in <module>
request.add_header("User-agent","Mozilla/5.0")
AttributeError: addinfourl instance has no attribute 'add_header'
从第二种方法就运行不出来啦,请教怎么解决啊? 用的python2.7版本
下面是我的代码
#coding=utf-8
import urllib2
import cookielib
url = "http://www.baidu.com"
print '第一种方法'
response1= urllib2.urlopen(url)
print response1.getcode()
print len(response1.read())
print "第二种方法"
request = urllib2.urlopen(url)
request.add_header("User-agent","Mozilla/5.0")
response2= urllib2.urlopen(url)
print response2.getcode()
print len(response2.read())
print "第三种方法"
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
response3= urllib2.urlopen(url)
print response3.getcode()
print len(response3.read())
print cj
print response3.read()