运行没错误 但是爬取数据为空值?
#-*- coding:utf-8 -
import urllib.request
from http import cookiejar
url = "https://www.baidu.com/"
print('-------1111----------')
response1 = urllib.request.urlopen(url)
print(response1.getcode())
print (len(response1.read()))
print (response1.read())
print('--------22222-----------')
request1 =urllib.request.Request(url)
request1.add_header("user-agent", "Mozilla/5.0")
response2 = urllib.request.urlopen(request1)
print(response2.getcode())
print (len(response2.read()))
print (response1.read())
print('---------333333333----------')
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 ('len:'+str(len(response3.read())))
print (response3.read())
#print (cj)
控制台输出结果为:
-------1111----------
200
227
b''
--------22222-----------
200
227
b''
---------333333333----------
200
len:227
b''