AttributeError:'模块'对象没有属性'urlopen'


146

我正在尝试使用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'

我在这里遵循指南: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。

Answers:


245

这适用于Python2.x。

对于Python 3,请在docs中查看:

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)

3
嗨,Eumiro,在Python中使用“ with”语句,我猜想它一旦完成使用就会自动关闭连接?类似于C#中的use语句?

@Sergio:完全是!通过缩进,您可以看到文件仍在何处打开。
eumiro 2010年

您好@eumiro,我输入时出现错误“ IndentationError:期望缩进的块” s = url.read(),请问我该如何解决?x
陈慧伦

@KarenChan您之前没有缩进s=url.read();之前有4个空格吗?
numbermaniac

19

与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)

1
with urlopen("http://www.python.org") as url:在的python2中不起作用AttributeError: addinfourl instance has no attribute '__exit__'。需要写url = urlopen("http://www.python.org")
orshachar '18

15
import urllib.request as ur
s = ur.urlopen("http://www.google.com")
sl = s.read()
print(sl)

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


7

为了使“ dataX = urllib.urlopen(url).read() ”在python 3中 工作(这对于python 2来说是正确的),您只需更改2个小东西即可。

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

dataX = urllib.request.urlopen(url).read()

2:其前面的import语句(从“ import urlib”更改为:

import urllib.request

它应该在python3中工作:)


3
import urllib.request as ur

filehandler = ur.urlopen ('http://www.google.com')
for line in filehandler:
    print(line.strip())

1

对于python 3,请尝试如下操作:

import urllib.request
urllib.request.urlretrieve('http://crcv.ucf.edu/THUMOS14/UCF101/UCF101/v_YoYo_g19_c02.avi', "video_name.avi")

它将视频下载到当前工作目录

我从这里得到帮助


1

python3的解决方案:

from urllib.request import urlopen

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

对于初学者来说简单易懂。谢谢
SHR

1

更改两行:

import urllib.request #line1

#Replace
urllib.urlopen("http://www.python.org")
#To
urllib.request.urlopen("http://www.python.org") #line2

如果收到错误403:禁止错误,请尝试以下操作:

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()

希望您的问题得到解决。


0

可能的方法之一:

import urllib
...

try:
    # Python 2
    from urllib2 import urlopen
except ImportError:
    # Python 3
    from urllib.request import urlopen


0

您在python2.x中使用的代码,可以这样使用:

from urllib.request import urlopen
urlopen(url)

顺便说一句,建议另一个名为的模块requests使用起来更友好,您可以使用pipinstall来安装,并像这样使用:

import requests
requests.get(url)
requests.post(url)

我以为它很容易使用,我也是初学者....哈哈


-1
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)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.