Python urllib urlopen 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25863101/
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
Python urllib urlopen not working
提问by Matilda Yi Pan
I am just trying to fetch data from a live web by using the urllib module, so I wrote a simple example
我只是想通过使用 urllib 模块从实时网络中获取数据,所以我写了一个简单的例子
Here is my code:
这是我的代码:
import urllib
sock = urllib.request.urlopen("http://diveintopython.org/")
htmlSource = sock.read()
sock.close()
print (htmlSource)
But I got error like:
但我得到了如下错误:
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'
采纳答案by Martijn Pieters
You are reading the wrong documentation or the wrong Python interpreter version. You tried to use the Python 3 library in Python 2.
您正在阅读错误的文档或错误的 Python 解释器版本。您尝试在 Python 2 中使用 Python 3 库。
Use:
用:
import urllib2
sock = urllib2.urlopen("http://diveintopython.org/")
htmlSource = sock.read()
sock.close()
print htmlSource
The Python 2 urllib2librarywas replaced by urllib.requestin Python 3.
Python 2urllib2库urllib.request在 Python 3 中被替换。
回答by yamm
This is what i use to get data from urls, its nice because you could save the file at the same time if you need it:
这是我用来从 url 获取数据的方法,这很好,因为如果需要,您可以同时保存文件:
import urllib
result = urllib.urlretrieve("http://diveintopython.org/")
print open(result[0]).read()
output:
输出:
'<!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>'
Edit: urlretrieve works in python 2 and 3
编辑:urlretrieve 在 python 2 和 3 中工作
回答by Mostafa Ezz
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()
回答by brada
In Python3you can use urllibor urllib3
在Python3 中,您可以使用urllib或urllib3
urllib:
网址:
import urllib.request
with urllib.request.urlopen('http://docs.python.org') as response:
htmlSource = response.read()
urllib3:
urllib3:
import urllib3
http = urllib3.PoolManager()
r = http.request('GET', 'http://docs.python.org')
htmlSource = r.data
More details could be found in the urllibor pythondocumentation.
回答by Seth Okeyo
Make sure you import requestsfrom urllib, then try this format, it worked for me:
确保您requests从导入urllib,然后尝试这种格式,它对我有用:
from urllib import request
urllib.request.urlopen( )
回答by Rajpurohit Dhanpal Singh
Use thisimport cv2
import numpy as np
import urllib //import urllib using pip
import requests // import requests using pipenter code hereurl = "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')
使用这个import cv2 import numpy as np import urllib //import urllib using pip import requests // 使用 pip enter code hereurl = "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')
回答by D.L
I just queried the same question which is now over 5 years old.
我刚刚问了同样的问题,现在已经超过 5 年了。
Please note that the URL given is also old, so i substituted the python welcome page.
请注意,给出的 URL 也是旧的,所以我替换了python 欢迎页面。
We can use the requests modulein python 3.
我们可以在python 3 中使用requests 模块。
I use python 3and the solution is below:
我使用python 3,解决方案如下:
import requests
r = requests.get('https://www.python.org/')
t = r.text
print(t)
This works and is clean.
这有效并且很干净。
回答by The Dan
For python 3 the correct way should be:
对于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()
Here you can find the documentation related to urllib.request
在这里您可以找到与urllib.request相关的文档

