为了账号安全,请及时绑定邮箱和手机立即绑定

运行没错误 但是爬取数据为空值?

#-*- 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''


正在回答

1 回答

https 换为http

0 回复 有任何疑惑可以回复我~
#1

慕尼黑9505207

写出http后,数据长度正确,但是打印出的内容依然为空。。。什么原因呢?
2019-07-09 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Python开发简单爬虫
  • 参与学习       227734    人
  • 解答问题       1218    个

本教程带您解开python爬虫这门神奇技术的面纱

进入课程

运行没错误 但是爬取数据为空值?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信