Python 属性错误:';模块';对象没有属性';urlopen';

Python 属性错误:';模块';对象没有属性';urlopen';,python,python-3.x,urllib,Python,Python 3.x,Urllib,我试图使用Python下载网站的HTML源代码,但我收到了这个错误 Traceback (most recent call last): File "C:\Users\Sergio.Tapia\Documents\NetBeansProjects\DICParser\src\WebDownload.py", line 3, in <module> file = urllib.urlopen("http://www.python.org") AttributeEr

我试图使用Python下载网站的HTML源代码,但我收到了这个错误

Traceback (most recent call last):  
    File "C:\Users\Sergio.Tapia\Documents\NetBeansProjects\DICParser\src\WebDownload.py", line 3, in <module>
     file = urllib.urlopen("http://www.python.org")
AttributeError: 'module' object has no attribute 'urlopen'

我正在使用Python3。

这在Python2.x中有效

对于Python 3,请查看:


在Python v3中,“urllib.request”本身是一个模块,因此此处不能使用“urllib”。

与Python 2+3兼容的解决方案是:

import urllib.request as ur

filehandler = ur.urlopen ('http://www.google.com')
for line in filehandler:
    print(line.strip())
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)
要获得“dataX=urllib.urlopen(url.read()”,使用python3(这对于python2)是正确的),您只需更改两件小事

1:urllib语句本身(在中间添加.request):

2:前面的导入语句(从“导入urlib”更改为:

import urllib.request

它应该在python3中工作:)

对于python3,请尝试以下操作:

import urllib.request
urllib.request.urlretrieve('http://crcv.ucf.edu/THUMOS14/UCF101/UCF101/v_YoYo_g19_c02.avi', "video_name.avi")
from urllib.request import urlopen
urlopen(url)
import requests
requests.get(url)
requests.post(url)
它将把视频下载到当前的工作目录

python3的解决方案:

from urllib.request import urlopen

url = 'http://www.python.org'
file = urlopen(url)
html = file.read()
print(html)

您在python2.x中使用的代码可以如下使用:

import urllib.request
urllib.request.urlretrieve('http://crcv.ucf.edu/THUMOS14/UCF101/UCF101/v_YoYo_g19_c02.avi', "video_name.avi")
from urllib.request import urlopen
urlopen(url)
import requests
requests.get(url)
requests.post(url)
顺便说一下,建议另一个名为
requests
的模块使用起来更友好,您可以使用
pip
安装它,并像这样使用:

import urllib.request
urllib.request.urlretrieve('http://crcv.ucf.edu/THUMOS14/UCF101/UCF101/v_YoYo_g19_c02.avi', "video_name.avi")
from urllib.request import urlopen
urlopen(url)
import requests
requests.get(url)
requests.post(url)

我认为它很容易使用,我也是初学者……哈哈

一种可能的方法:

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)
import urllib
...

try:
    # Python 2
    from urllib2 import urlopen
except ImportError:
    # Python 3
    from urllib.request import urlopen
更改两行:

import urllib.request #line1

#Replace
urllib.urlopen("http://www.python.org")
#To
urllib.request.urlopen("http://www.python.org") #line2
如果出现错误403:禁止的错误异常,请尝试以下操作:


我希望您的问题得到解决。

使用六个模块使您的代码在python2python3之间兼容

urllib.request.urlopen("<your-url>")```
urllib.request.urlopen(“”)```

在使用urlopen之前添加这个
RequestMethods
,Hi-Eumiro,在Python中使用'with'语句,我猜它会在使用完后自动关闭连接吗?与C#?@Sergio中的use语句类似:没错!通过缩进,您可以看到文件仍然打开的位置。您好@eumiro,我在键入
s=url.read()
时出现了一个错误“缩进错误:预期缩进块”,请问如何解决?x@KarenChan您缺少
s=url.read()前面的缩进;您之前有4个空格吗?当我将您的方法
与urlopen(“http://www.python.org)作为url:
在具有
属性的python2中不起作用错误:AddInfo url实例没有属性“\uuu exit”
。需要写入
url=urlopen(“http://www.python.org)
简单易懂,适合初学者。感谢您可以通过这种方式从six.moves导入六个模块导入urllib