urllib.request的用法
一直报错,说:AttributeError: module 'urllib' has no attribute 'request'这是为什么?哪里错了?
import urllib
def askURL(url):
head={
'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'
}
request = urllib.request.Request(url, headers=head)
html = ''
try:
response = urllib.request.urlopen(request)
html = response.read().decode('utf-8')
print(html)
except urllib.error.URLError as e:
print(e.reason)
askURL('https://movie.douban.com/top250?start=0') 本帖最后由 临时号 于 2022-6-9 21:43 编辑
import urllib.request
import urllib.error
def askURL(url):
head={
'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'
}
request = urllib.request.Request(url, headers=head)
html = ''
try:
response = urllib.request.urlopen(request)
html = response.read().decode('utf-8')
print(html)
except urllib.error.URLError as e:
print(e.reason)
askURL('https://movie.douban.com/top250?start=0') 临时号 发表于 2022-6-9 21:30
为什么不能直接导入urllib库呢?这个库里不但有request模块,还有error模块呀,万一有个什么error,只导入request就不能用呀 如果有error也只能一个个导
你看小甲鱼的代码的不也不能直接导库吗 fledgling 发表于 2022-6-9 21:33
为什么不能直接导入urllib库呢?这个库里不但有request模块,还有error模块呀,万一有个什么error,只导 ...
在python3中urllib模块导入不能直接import urllib 而要import urllib .request
页:
[1]