urllib2.urlopen()打开mp3链接时,报错


请教各位,准备在VOA的官网上正常mp3文件,在用 urllib2.urlopen()下载mp3类型文件时,报了 URLError。有可能是哪些原因造成的呢?多谢各位。

使用的代码如下:


 urllib2.urlopen('http://realaudio.rferl.org/voa/LERE/2013/07/19/6d07939b-45fc-4984-92b5-7f9ccc978ac5.mp3')

已经进行的一些尝试:

  1. 链接是浏览器中是可以正常访问的。
  2. 在chrome中修改user-agent为'aaa',也可以正常下载,应该可以排除网站限制了非浏览器访问。
  3. 下载html类型的链接正常。

感谢@依云的提醒,以下是Traceback:


 >>> urllib2.urlopen('http://realaudio.rferl.org/voa/LERE/2013/07/19/6d07939b-45fc-4984-92b5-7f9ccc978ac5.mp3')

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    urllib2.urlopen('http://realaudio.rferl.org/voa/LERE/2013/07/19/6d07939b-45fc-4984-92b5-7f9ccc978ac5.mp3')
  File "C:\Python27\lib\urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 404, in open
    response = self._open(req, data)
  File "C:\Python27\lib\urllib2.py", line 422, in _open
    '_open', req)
  File "C:\Python27\lib\urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 1214, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "C:\Python27\lib\urllib2.py", line 1187, in do_open
    r = h.getresponse(buffering=True)
  File "C:\Python27\lib\httplib.py", line 1045, in getresponse
    response.begin()
  File "C:\Python27\lib\httplib.py", line 409, in begin
    version, status, reason = self._read_status()
  File "C:\Python27\lib\httplib.py", line 365, in _read_status
    line = self.fp.readline(_MAXLINE + 1)
  File "C:\Python27\lib\socket.py", line 476, in readline
    data = self._sock.recv(self._rbufsize)
error: [Errno 10054]

urllib2 python

螺螺螺螺螺螺螺 11 years, 5 months ago

代码最先是在Windows下运行的。尝试在linux运行时,报错信息中有“Connection been reset by peers”,所以推测是GFW的问题。

到linode机器上验证了下,运行OK。

结论:这个问题是GFW的问题,跟代码没什么关系。

Saber.L answered 11 years, 5 months ago

Your Answer