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

python3环境下第三种方式实现不了,求大佬指教

import urllib.request
print("第一种抓取链接的内容的方法:")
url='http://www.baidu.com'
response1=urllib.request.urlopen(url)
html=response1.read()
print(response1.getcode())#获取http状态码
print(len(html))#返回爬取内容的长度
print(html.decode('utf-8'))#输入百度网页对应的代码

import urllib.request
print("第二种抓取链接的内容的方法:")
url='http://www.baidu.com'
request=urllib.request.Request(url)
request.add_header("User_Agent","Mozilla/5.0")
response2=urllib.request.urlopen(url)
html=response2.read()
print(response2.getcode())#获取http状态码
print(len(html))#返回爬取内容的长度
print(html.decode('utf-8'))#输入百度网页对应的代码

import urllib.request
from http import cookiejar
print('第三种抓取链接内容的方法')
url='http://www.baidu.com'
cj=cookiejar.CookieJar
opener=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
urllib.request.install_opener(opener)
response3=urllib.request.urlopen(url)
html=response3.read()
print(response3.getcode())#获取http状态码
print(len(html))#返回爬取内容的长度
print(html.decode('utf-8'))#输入百度网页对应的代码


正在回答

3 回答

cj=cookiejar.CookieJar()


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

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

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

第三个代码出错提示,表示不解

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

举报

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

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

进入课程

python3环境下第三种方式实现不了,求大佬指教

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