Python urllib urlopen not working
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()
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.