属性错误:“模块”对象没有属性“urlopen”;

发布于 2024-09-28 07:32:48 字数 672 浏览 7 评论 0原文

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

深者入戏 2024-10-05 07:32:49

可能的方法之一:

import urllib
...

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

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
舟遥客 2024-10-05 07:32:49

如果您的代码使用Python 2.x版本,您可以执行以下操作:

from urllib.request import urlopen
urlopen(url)

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

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

If your code uses Python version 2.x, you can do the following:

from urllib.request import urlopen
urlopen(url)

By the way, I suggest another module called requests, which is more friendly to use. You can use pip install it, and use it like this:

import requests
requests.get(url)
requests.post(url)
我们的影子 2024-10-05 07:32:49

使用第三方six模块让你的代码在Python2之间兼容和Python3。

from six.moves import urllib
urllib.request.urlopen("<your-url>")

Use the third-party six module to make your code compatible between Python2 and Python3.

from six.moves import urllib
urllib.request.urlopen("<your-url>")
埋情葬爱 2024-10-05 07:32:49
imgResp = urllib3.request.RequestMethods.urlopen(url)

在使用 urlopen 之前添加此 RequestMethods

imgResp = urllib3.request.RequestMethods.urlopen(url)

Add this RequestMethods before using urlopen

如日中天 2024-10-05 07:32:48

这适用于 Python 2.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)

This works in Python 2.x.

For Python 3 look in the 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)
南街九尾狐 2024-10-05 07:32:48

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)

A Python 2+3 compatible solution is:

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)
养猫人 2024-10-05 07:32:48
import urllib.request as ur
s = ur.urlopen("http://www.google.com")
sl = s.read()
print(sl)

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

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.

只是我以为 2024-10-05 07:32:48

让 'dataX = urllib.urlopen(url).read()' 在 python3 中工作(这是正确的对于 python2) 你必须只改变 2 个小东西。

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

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

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

import urllib.request

并且它应该在 python3 中工作:)

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

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

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

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

import urllib.request

And it should work in python3 :)

眼睛会笑 2024-10-05 07:32:48

更改两行:

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

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

Change TWO lines:

import urllib.request #line1

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

If You got ERROR 403: Forbidden Error exception try this:

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

I hope your problem resolved.

指尖微凉心微凉 2024-10-05 07:32:48
import urllib.request as ur

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

filehandler = ur.urlopen ('http://www.google.com')
for line in filehandler:
    print(line.strip())
甜`诱少女 2024-10-05 07:32:48

对于 python 3,请尝试这样的操作:

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

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

我从这里获得帮助

For python 3, try something like this:

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

菩提树下叶撕阳。 2024-10-05 07:32:48

python3的解决方案:

from urllib.request import urlopen

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

Solution for python3:

from urllib.request import urlopen

url = 'http://www.python.org'
file = urlopen(url)
html = file.read()
print(html)
瞎闹 2024-10-05 07:32:48
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
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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文