Question

I made this python lib and it had this function with uses urllib and urllib2 but when i execute the lib's functions from python shell i get this error

>>> from sabermanlib import geturl
>>> geturl("roblox.com","ggg.html")

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    geturl("roblox.com","ggg.html")
  File "sabermanlib.py", line 21, in geturl
    urllib.urlretrieve(Address,File)
  File "C:\Users\Andres\Desktop\ddd\Portable Python 2.7.5.1\App\lib\urllib.py", line 94, in urlretrieve
    return _urlopener.retrieve(url, filename, reporthook, data)
  File "C:\Users\Andres\Desktop\ddd\Portable Python 2.7.5.1\App\lib\urllib.py", line 240, in retrieve
    fp = self.open(url, data)
  File "C:\Users\Andres\Desktop\ddd\Portable Python 2.7.5.1\App\lib\urllib.py", line 208, in open
    return getattr(self, name)(url)
  File "C:\Users\Andres\Desktop\ddd\Portable Python 2.7.5.1\App\lib\urllib.py", line 463, in open_file
    return self.open_local_file(url)
  File "C:\Users\Andres\Desktop\ddd\Portable Python 2.7.5.1\App\lib\urllib.py", line 477, in open_local_file
    raise IOError(e.errno, e.strerror, e.filename)
IOError: [Errno 2] The system cannot find the file specified: 'roblox.com'
>>>

and here's the code for the lib i made:

import urllib
import urllib2




def geturl(Address,File):
    urllib.urlretrieve(Address,File)

EDIT 2

I cant understand why i get this error in the python shell executing:

geturl(Address,File)
Was it helpful?

Solution

You don't want urllib.urlretrieve. This takes a file-like object. Instead, you want urllib.urlopen:

>>> help(urllib.urlopen)
urlopen(url, data=None, proxies=None)
    Create a file-like object for the specified URL to read from.

Additionally, if you want to download and save a document, you'll need a more robust geturl function:

def geturl(Address, FileName):
    html_data = urllib.urlopen(Address).read()  # Open the URL
    with open(FileName, 'wb') as f:  # Open the file
        f.write(html_data)  # Write data from URL to file

geturl(u'http://roblox.com')  # URL's must contain the full URI, including http://
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top