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

请问方法二的"request"请求和方法三的“cj"容器哪儿错了?

import urllib.request    # Python之后urllib2改为urllib.request

print("方法一")
url = "https://dxy.com/faq"
response_1 = urllib.request.urlopen(url)
print(response_1.getcode())    # 截取状态码,是200则成功
# print(response_1.read())     # 未解码
print(len(response_1.read()))

print("\n方法二")
request = urllib.request(url)      # 错误一
request.add_header("user-agent","Mozilla/5.0 ")    # 将爬虫伪装成浏览器
response_2 = urllib.request.urlopen(request)
print(response_2.getcode())
print(len(response_2.read()))

print("\n方法三")
cj = cookielib.CookieJar()         # 错误二
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
urllib.request.install_opener(opener)
response_3 = urllib.request.urlopen(url)
print(response_3.getcode())    # 截取状态码,是200则成功
print cj
print(len(response_3.read()))


正在回答

2 回答

第三个地方 我还用了了 

import http.cookiejar
cj = http.cookiejar.CookieJar()


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

慕粉7380412 提问者

万分感谢!
2019-08-30 回复 有任何疑惑可以回复我~
request = urllib.request(url)      # 错误一

这里是request = urllib.request.Request(url)  #返回的是一个request对象

cj = cookielib.CookieJar()         # 错误二

https://img1.sycdn.imooc.com//5d67992100013b9f07600046.jpg

https://img1.sycdn.imooc.com//5d67993e0001842e04910078.jpg

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

举报

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

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

进入课程

请问方法二的"request"请求和方法三的“cj"容器哪儿错了?

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