鱼C论坛

 找回密码
 立即注册
查看: 1221|回复: 4

[已解决]urllib.request的用法

[复制链接]
发表于 2022-6-9 21:16:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
一直报错,说:AttributeError: module 'urllib' has no attribute 'request'
这是为什么?哪里错了?
  1. import urllib
  2. def askURL(url):
  3.     head={
  4.         'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36 Edg/102.0.1245.33'
  5.     }
  6. request = urllib.request.Request(url, headers=head)
  7. html = ''
  8. try:
  9.     response = urllib.request.urlopen(request)
  10.     html = response.read().decode('utf-8')
  11.     print(html)
  12. except urllib.error.URLError as e:
  13.     print(e.reason)


  14. askURL('https://movie.douban.com/top250?start=0')
复制代码
最佳答案
2022-6-9 21:30:25
本帖最后由 临时号 于 2022-6-9 21:43 编辑
  1. import urllib.request
  2. import urllib.error
  3. def askURL(url):
  4.         head={
  5.         'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36 Edg/102.0.1245.33'
  6.         }
  7.         request = urllib.request.Request(url, headers=head)
  8.         html = ''
  9.         try:
  10.                 response = urllib.request.urlopen(request)
  11.                 html = response.read().decode('utf-8')
  12.                 print(html)
  13.         except urllib.error.URLError as e:
  14.                 print(e.reason)


  15. askURL('https://movie.douban.com/top250?start=0')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-6-9 21:30:25 | 显示全部楼层    本楼为最佳答案   
本帖最后由 临时号 于 2022-6-9 21:43 编辑
  1. import urllib.request
  2. import urllib.error
  3. def askURL(url):
  4.         head={
  5.         'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36 Edg/102.0.1245.33'
  6.         }
  7.         request = urllib.request.Request(url, headers=head)
  8.         html = ''
  9.         try:
  10.                 response = urllib.request.urlopen(request)
  11.                 html = response.read().decode('utf-8')
  12.                 print(html)
  13.         except urllib.error.URLError as e:
  14.                 print(e.reason)


  15. askURL('https://movie.douban.com/top250?start=0')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-6-9 21:33:10 | 显示全部楼层

为什么不能直接导入urllib库呢?这个库里不但有request模块,还有error模块呀,万一有个什么error,只导入request就不能用呀
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-6-9 21:36:09 | 显示全部楼层
如果有error也只能一个个导
你看小甲鱼的代码的不也不能直接导库吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-6-9 21:44:41 | 显示全部楼层
fledgling 发表于 2022-6-9 21:33
为什么不能直接导入urllib库呢?这个库里不但有request模块,还有error模块呀,万一有个什么error,只导 ...

在python3中urllib模块导入不能直接import urllib 而要import urllib .request
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-28 10:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表