Mp3下载urllib.request.urlopen()超时

时间:2016-01-26 20:41:06

标签: python python-3.x urllib urlopen

我有一个名为href_w的列表,下载了一堆mp3链接,但是当我执行这段代码时,它给了我一个错误:

# Download file
print(color.BLUE + "\n[*] Downloading requested mp3(s) ..." + color.END)
for link in href_w:
    url = "http://www.mp3c.cc"+link.replace('track','files')
    print("[*] GET "+url)
    req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
    response = urlopen(req)
    html = response.read()

错误日志如下所示:

[*] Downloading requested mp3(s) ...
[*] GET http://www.mp3c.cc/files/88144527_288384031/
Traceback (most recent call last):
  File "mp3spyder.py", line 99, in <module>
    response = urlopen(req)
  File "/usr/lib/python3.4/urllib/request.py", line 153, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.4/urllib/request.py", line 461, in open
    response = meth(req, response)
  File "/usr/lib/python3.4/urllib/request.py", line 571, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python3.4/urllib/request.py", line 493, in error
    result = self._call_chain(*args)
  File "/usr/lib/python3.4/urllib/request.py", line 433, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.4/urllib/request.py", line 676, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "/usr/lib/python3.4/urllib/request.py", line 461, in open
    response = meth(req, response)
  File "/usr/lib/python3.4/urllib/request.py", line 571, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python3.4/urllib/request.py", line 499, in error
    return self._call_chain(*args)
  File "/usr/lib/python3.4/urllib/request.py", line 433, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.4/urllib/request.py", line 579, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 502: Bad Gateway

我能做些什么来做对吗?

2 个答案:

答案 0 :(得分:1)

如果您从Web服务器收到502响应,通常意味着他们的后端已关闭。如果你使用curl或wget,你可能会得到相同的响应。

无论如何,你应该总是尝试使用curl或wget来调试这些问题。

答案 1 :(得分:0)

我使用了这个脚本。它获取文件的url,读取并保存到文件中。这是下载mp3的示例。

import urllib

url = 'http://www.drowtales.com/download/Path%20to%20Power.mp3'

data = urllib.request.urlopen(url)

f = open('Path to Power.mp3','wb')
f.write(data.read())
f.close()