2013-10-25 19 views
-1

我做了这条巨蟒lib和它与使用的urllib和urllib2的这个功能,但是当我在Python shell中执行的lib的功能,我得到这个错误Python的lib中执行错误

>>> 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' 
>>> 

,这里是对的lib代码我提出:

import urllib 
import urllib2 




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

EDIT 2

我无法理解,为什么我在Python Shell中执行此错误:

geturl(Address,File) 

回答

0

你不想urllib.urlretrieve。这需要一个类似文件的对象。相反,你要了urllib.urlopen:

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

此外,如果您想要下载和保存文档时,你需要一个更强大的getURL功能:

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://