不能用python下载文件但我可以从浏览器下载

时间:2016-12-14 16:25:16

标签: python url download urllib2

我正在尝试创建一个从互联网上下载文件的功能。如果我转到直接网址,我会获取图像或下载文件。但是,当我运行我的代码时,它只是挂起然后我得到超时错误。有什么特别的原因可能会发生这种情况吗?

#fpp = "http://www.blog.pythonlibrary.org/wp-content/uploads/2012/06/wxDbViewer.zip"
fpp = "http://www.gunnerkrigg.com//comics/00000001.jpg"
download_file(fpp)

这是我的功能:

import urllib2

def download_file(url_path):
    response = urllib2.urlopen(url_path)
    data = response.read()

有什么特别的理由可以使用浏览器而不是代码吗?

这是我得到的错误:

追踪(最近一次呼叫最后一次):

  File "/Users/dk/testing/myfile.py", line 42, in <module>
    download_file(fpp)
  File "/Users/dk/Documents/testing/code_project.py", line 154, in download_file
    response = urllib2.urlopen(url_path)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 431, in open
    response = self._open(req, data)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 449, in _open
    '_open', req)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1227, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1197, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno 60] Operation timed out>

0 个答案:

没有答案