输出结果和老师的不一样啊
import urllib
from pip._vendor.requests.packages import urllib3
from http.cookiejar import Cookie
from http import cookiejar
url = 'http://www.baidu.com/'
print("the first")
response1 = urllib.request.urlopen(url)
print (response1.getcode())
print (len(response1.read()))
print('the second')
request = urllib.request.Request(url)
request.add_header("user-agent", "Mozilla/5.0")
response2 = urllib.request.urlopen(request)
print( response2.getcode())
print (len(response2.read()))
print('the third')
cj = cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
urllib.request.install_opener(opener)
response3 = urllib.request.urlopen(url)
print (response3.getcode())
print (cj)
print (len(response3.read()))
输出的结果是:
the first
200
2783
the second
200
98558
the third
200
<CookieJar[<Cookie qh[360]=1 for .baidu.com/>]>
2783