AttributeError:'module' 对象没有属性 'urlopen'
- 2025-03-05 09:15:00
- admin 原创
- 3
问题描述:
我正在尝试使用 Python 下载网站的 HTML 源代码,但收到此错误。
Traceback (most recent call last):
File "C:UsersSergio.TapiaDocumentsNetBeansProjectsDICParsersrcWebDownload.py", line 3, in <module>
file = urllib.urlopen("http://www.python.org")
AttributeError: 'module' object has no attribute 'urlopen'
我正在遵循这里的指南:http://www.boddie.org.uk/python/HTML.html
import urllib
file = urllib.urlopen("http://www.python.org")
s = file.read()
f.close()
#I'm guessing this would output the html source code?
print(s)
我正在使用 Python 3。
解决方案 1:
这在 Python 2.x 中有效。
对于 Python 3,请查看文档:
import urllib.request
with urllib.request.urlopen("http://www.python.org") as url:
s = url.read()
# I'm guessing this would output the html source code ?
print(s)
解决方案 2:
与 Python 2+3 兼容的解决方案是:
import sys
if sys.version_info[0] == 3:
from urllib.request import urlopen
else:
# Not Python 3 - today, it is most likely to be Python 2
# But note that this might need an update when Python 4
# might be around one day
from urllib import urlopen
# Your code where you can use urlopen
with urlopen("http://www.python.org") as url:
s = url.read()
print(s)
解决方案 3:
import urllib.request as ur
s = ur.urlopen("http://www.google.com")
sl = s.read()
print(sl)
在 Python v3 中,“urllib.request”本身是一个模块,因此“urllib”不能在这里使用。
解决方案 4:
为了使 ' dataX = urllib.urlopen (url).read() ' 在 python 3中工作(这对于 python 2来说是正确的),您只需更改两处小细节。
1: urllib语句本身(中间添加.request):
dataX = urllib.request.urlopen(url).read()
2:其前面的导入语句(从“import urlib”更改为:
import urllib.request
它应该可以在 python3 中工作:)
解决方案 5:
更改两行:
import urllib.request #line1
#Replace
urllib.urlopen("http://www.python.org")
#To
urllib.request.urlopen("http://www.python.org") #line2
如果出现 ERROR 403: Forbidden Error 异常,请尝试以下操作:
siteurl = "http://www.python.org"
req = urllib.request.Request(siteurl, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.100 Safari/537.36'})
pageHTML = urllib.request.urlopen(req).read()
我希望你的问题得到了解决。
解决方案 6:
import urllib.request as ur
filehandler = ur.urlopen ('http://www.google.com')
for line in filehandler:
print(line.strip())
解决方案 7:
对于 Python 3,请尝试以下操作:
import urllib.request
urllib.request.urlretrieve('http://crcv.ucf.edu/THUMOS14/UCF101/UCF101/v_YoYo_g19_c02.avi', "video_name.avi")
它会将视频下载到当前工作目录
我从这里获得了帮助
解决方案 8:
python3 的解决方案:
from urllib.request import urlopen
url = 'http://www.python.org'
file = urlopen(url)
html = file.read()
print(html)
解决方案 9:
import urllib
import urllib.request
from bs4 import BeautifulSoup
with urllib.request.urlopen("http://www.newegg.com/") as url:
s = url.read()
print(s)
soup = BeautifulSoup(s, "html.parser")
all_tag_a = soup.find_all("a", limit=10)
for links in all_tag_a:
#print(links.get('href'))
print(links)
解决方案 10:
其中一种可能的方法:
import urllib
...
try:
# Python 2
from urllib2 import urlopen
except ImportError:
# Python 3
from urllib.request import urlopen
解决方案 11:
如果您的代码使用 Python 版本 2.x,您可以执行以下操作:
from urllib.request import urlopen
urlopen(url)
顺便说一句,我建议使用另一个名为的模块requests
,它使用起来更友好。您可以pip
安装它,然后像这样使用它:
import requests
requests.get(url)
requests.post(url)
解决方案 12:
使用第三方six
模块让你的代码兼容Python2和Python3。
from six.moves import urllib
urllib.request.urlopen("<your-url>")
解决方案 13:
imgResp = urllib3.request.RequestMethods.urlopen(url)
RequestMethods
在使用 urlopen 之前添加
相关推荐
热门文章
项目管理软件有哪些?
- 2025年20款好用的项目管理软件推荐,项目管理提效的20个工具和技巧
- 2024年开源项目管理软件有哪些?推荐5款好用的项目管理工具
- 2024年常用的项目管理软件有哪些?推荐这10款国内外好用的项目管理工具
- 项目管理软件有哪些?推荐7款超好用的项目管理工具
- 项目管理软件有哪些最好用?推荐6款好用的项目管理工具
- 项目管理软件哪个最好用?盘点推荐5款好用的项目管理工具
- 项目管理软件排行榜:2024年项目经理必备5款开源项目管理软件汇总
- 项目管理必备:盘点2024年13款好用的项目管理软件
- 项目管理软件有哪些,盘点推荐国内外超好用的7款项目管理工具
- 2024项目管理软件排行榜(10类常用的项目管理工具全推荐)
热门标签
云禅道AD