Python的urllib.urlopen无法工作

16

我只是想使用urllib模块从实时网站获取数据,所以我写了一个简单的示例。

这是我的代码:

import urllib

sock = urllib.request.urlopen("http://diveintopython.org/") 
htmlSource = sock.read()                            
sock.close()                                        
print (htmlSource)  

但我收到了错误消息:

Traceback (most recent call last):
  File "D:\test.py", line 3, in <module>
    sock = urllib.request.urlopen("http://diveintopython.org/") 
AttributeError: 'module' object has no attribute 'request'
8个回答

26

你正在阅读错误的文档或错误的Python解释器版本。你试图在Python 2中使用Python 3库。

请使用:

import urllib2

sock = urllib2.urlopen("http://diveintopython.org/") 
htmlSource = sock.read()                            
sock.close()                                        
print htmlSource

Python 3 中用于网络请求的库 urllib2urllib.request 取代。


7
import requests
import urllib

link = "http://www.somesite.com/details.pl?urn=2344"

f = urllib.request.urlopen(link)
myfile = f.read()

writeFileObj = open('output.xml', 'wb')
writeFileObj.write(myfile)
writeFileObj.close()

4
一个好回答通常不仅包含代码,还会对具体修复了什么进行一些解释。 - Loïc Faure-Lacroix
1
这个回答中你并没有使用requests模块,它不是一个不同的模块吗? - Rick

6

In Python3 you can use urllib or urllib3

urllib:

import urllib.request
with urllib.request.urlopen('http://docs.python.org') as response:
    htmlSource = response.read()

urllib3:

import urllib3
http = urllib3.PoolManager()
r = http.request('GET', 'http://docs.python.org')
htmlSource = r.data

更多细节可以在urllibpython文档中找到。


3

这是我用来从URL获取数据的方法,它很好用,因为如果需要的话,可以同时保存文件:

import urllib

result = urllib.urlretrieve("http://diveintopython.org/")

print open(result[0]).read()

输出:

'<!DOCTYPE html><body style="padding:0; margin:0;"><iframe src="http://mcc.godaddy.com/park/pKMcpaMuM2WwoTq1LzRhLzI0" style="visibility: visible;height: 2000px;" allowtransparency="true" marginheight="0" marginwidth="0" frameborder="0" scrolling="no" width="100%"></iframe></body></html>'

编辑:urlretrieve在Python 2和3中均可使用


urlretrieve 在 Python 3 中可用,同时也作为遗留接口的一部分。如果你只是想要将数据保存在内存中,那么 urlretrieve 就不是正确的工具;因为它会将数据下载到磁盘上,然后再从文件中打开。 - Martijn Pieters
1
此外,这甚至没有试图回答所提出的问题。 - Martijn Pieters
我说urlretrieve很好,因为你可以保存文件...这就是全部意义。如果有人告诉我他想从互联网获取数据,为什么不建议像这样的东西呢。 - yamm
我不知道它在Python 3中是否有效,尽管它可能已经过时或其他原因。;-) - yamm

0

对于Python 3,正确的方式应该是:

import cv2
import numpy as np
import urllib.request

req = urllib.request.urlopen('http://answers.opencv.org/upfiles/logo_2.png')
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
img = cv2.imdecode(arr, -1) # 'Load it as it is'

cv2.imshow('image_name', img)
if cv2.waitKey() & 0xff == 27: quit()

在这里,您可以找到与urllib.request相关的文档。


0

确保您从urllib导入requests,然后尝试使用这个格式,这对我起作用了:

from urllib import request
urllib.request.urlopen( )

0

我刚查询了一个超过5年的旧问题。

请注意,给出的URL也很旧,所以我替换了Python欢迎页面

我们可以在Python 3中使用requests模块

我使用Python 3,解决方案如下:

import requests

r = requests.get('https://www.python.org/')
t = r.text

print(t)

这个有效且整洁。


-1

使用这个

    import cv2
    import  numpy as np
    import urllib //import urllib using pip
    import requests // import requests using pip`enter code here`
    url = "write your url"
    while True:
    imgresp = urllib.request.urlopen(url)
    imgnp = np.array(bytearray(imgresp.read()),dtype=np.uint8)
    img = cv2.imdecode(imgnp,-1)
    cv2.imshow("test",img)
    cv2.waitKey('q')

2
如果您要发布答案,请尝试正确格式化代码 - 这样不易阅读。此外,似乎您并没有回答问题; 您正在使用numpy,而问题中没有提到它,然后似乎有一些被注释掉的导入,最后似乎是在下载图像,这也不是问题所要求的。 - DaveyDaveDave

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接