Python urllib urlopen not working

帅比萌擦擦* 提交于 2020-01-02 01:15:10

问题


I am just trying to fetch data from a live web by using the urllib module, so I wrote a simple example

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'

回答1:


You are reading the wrong documentation or the wrong Python interpreter version. You tried to use the Python 3 library in Python 2.

Use:

import urllib2

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

The Python 2 urllib2 library was replaced by urllib.request in Python 3.




回答2:


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



回答3:


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:

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




回答4:


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

More details could be found in the urllib or python documentation.




回答5:


Make sure you import requests from urllib, then try this format, it worked for me:

from urllib import request
urllib.request.urlopen( )



回答6:


Use this import cv2 import numpy as np import urllib //import urllib using pip import requests // import requests using pipenter 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')



来源:https://stackoverflow.com/questions/25863101/python-urllib-urlopen-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!