Python AttributeError: 'module' 对象没有属性 'urlopen'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3969726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:36:49  来源:igfitidea点击:

AttributeError: 'module' object has no attribute 'urlopen'

pythonpython-3.xurllib

提问by

I'm trying to use Python to download the HTML source code of a website but I'm receiving this error.

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

I'm following the guide here: http://www.boddie.org.uk/python/HTML.html

我正在按照这里的指南进行操作: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)

I'm using Python 3.

我正在使用 Python 3。

采纳答案by eumiro

This works in Python 2.x.

这适用于 Python 2.x。

For Python 3 look in the docs:

对于 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)

回答by Manu Mariaraj

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

In Python v3 the "urllib.request" is a module by itself, therefore "urllib" cannot be used here.

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

回答by Kamran

import urllib.request as ur

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

回答by Martin Thoma

A Python 2+3 compatible solution is:

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)

回答by Steven B. Peutz

To get 'dataX = urllib.urlopen(url).read()' working in python3(this would have been correct for python2)you must just change 2 little things.

要让“ dataX = urllib.urlopen(url).read()”在python 3中工作(这对于python 2来说是正确的),您必须只更改2个小东西。

1:The urllib statement itself (add the .request in the middle):

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

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

2:The import statement preceding it (change from 'import urlib' to:

2:它前面的导入语句(从 'import urlib' 更改为:

import urllib.request

And it should work in python3 :)

它应该在 python3 中工作:)

回答by rocksyne

For python 3, try something like this:

对于 python 3,尝试这样的事情:

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

It will download the video to the current working directory

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

I got help from HERE

我从这里得到了帮助

回答by Banjali

Solution for python3:

python3的解决方案:

from urllib.request import urlopen

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

回答by jason.lu

your code used in python2.x, you can use like this:

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

from urllib.request import urlopen
urlopen(url)

by the way , suggest another model called requests is more friendly to use, you can use pip install it, and use like this:

顺便说一句,建议使用另一个名为 requests 的模型更友好,您可以使用 pip install 它,并像这样使用:

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

I thought it is easily to use, i am beginner too....hahah

我认为它很容易使用,我也是初学者....hahah

回答by user11649630

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 Vasyl Lyashkevych

One of the possible way to do it:

一种可能的方法:

import urllib
...

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