使用 urllib2 下载种子文件
Downloading torrent file using urllib2
我正在尝试使用 urllib2 下载 torrent 文件,但出现错误:
Traceback (most recent call last):
File "download_torrent.py", line 11, in <module>
torr_file = urllib2.urlopen(url)
File "C:\Users\Aly Akhtar\AppData\Local\Continuum\Anaconda\lib\urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\Aly Akhtar\AppData\Local\Continuum\Anaconda\lib\urllib2.py", line 431, in open
response = self._open(req, data)
File "C:\Users\Aly Akhtar\AppData\Local\Continuum\Anaconda\lib\urllib2.py", line 449, in _open
'_open', req)
File "C:\Users\Aly Akhtar\AppData\Local\Continuum\Anaconda\lib\urllib2.py", line 409, in _call_chain
result = func(*args)
File "C:\Users\Aly Akhtar\AppData\Local\Continuum\Anaconda\lib\urllib2.py", line 1240, in https_open
context=self._context)
File "C:\Users\Aly Akhtar\AppData\Local\Continuum\Anaconda\lib\urllib2.py", line 1200, in do_open
r = h.getresponse(buffering=True)
File "C:\Users\Aly Akhtar\AppData\Local\Continuum\Anaconda\lib\httplib.py", line 1132, in getresponse
response.begin()
File "C:\Users\Aly Akhtar\AppData\Local\Continuum\Anaconda\lib\httplib.py", line 453, in begin
version, status, reason = self._read_status()
File "C:\Users\Aly Akhtar\AppData\Local\Continuum\Anaconda\lib\httplib.py", line 417, in _read_status
raise BadStatusLine(line)
httplib.BadStatusLine: ''
以下是我正在尝试的代码:
url = 'https://torcache.net/torrent/7E74F1E4AEDC08A2D63BE1EDF612AC0BF17ECBBD.torrent?title=[kat.cr]batman.v.superman.dawn.of.justice.2016.720p.hdtc.1.2gb.shaanig'
torr_file = urllib2.urlopen(url)
with open("mytorrent.torrent", "w") as f:
f.write(torr_file.read())
我已经经历了几个类似的问题,但找不到解决方案。
您尝试下载的网站似乎放弃了 header 中没有 User-Agent
字符串的请求。我在这里找到了解决您问题的方法:https://whosebug.com/a/9265980/1577207.
添加 User-Agent
header 为我解决了这个问题:
import urllib2
url = 'https://torcache.net/torrent/7E74F1E4AEDC08A2D63BE1EDF612AC0BF17ECBBD.torrent?title=[kat.cr]batman.v.superman.dawn.of.justice.2016.720p.hdtc.1.2gb.shaanig'
opener = urllib2.build_opener()
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 5.1; rv:10.0.1) Gecko/20100101 Firefox/10.0.1',
}
opener.addheaders = headers.items()
torr_file = opener.open(url)
with open("mytorrent.torrent", "w") as f:
f.write(torr_file.read())
我正在尝试使用 urllib2 下载 torrent 文件,但出现错误:
Traceback (most recent call last):
File "download_torrent.py", line 11, in <module>
torr_file = urllib2.urlopen(url)
File "C:\Users\Aly Akhtar\AppData\Local\Continuum\Anaconda\lib\urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\Aly Akhtar\AppData\Local\Continuum\Anaconda\lib\urllib2.py", line 431, in open
response = self._open(req, data)
File "C:\Users\Aly Akhtar\AppData\Local\Continuum\Anaconda\lib\urllib2.py", line 449, in _open
'_open', req)
File "C:\Users\Aly Akhtar\AppData\Local\Continuum\Anaconda\lib\urllib2.py", line 409, in _call_chain
result = func(*args)
File "C:\Users\Aly Akhtar\AppData\Local\Continuum\Anaconda\lib\urllib2.py", line 1240, in https_open
context=self._context)
File "C:\Users\Aly Akhtar\AppData\Local\Continuum\Anaconda\lib\urllib2.py", line 1200, in do_open
r = h.getresponse(buffering=True)
File "C:\Users\Aly Akhtar\AppData\Local\Continuum\Anaconda\lib\httplib.py", line 1132, in getresponse
response.begin()
File "C:\Users\Aly Akhtar\AppData\Local\Continuum\Anaconda\lib\httplib.py", line 453, in begin
version, status, reason = self._read_status()
File "C:\Users\Aly Akhtar\AppData\Local\Continuum\Anaconda\lib\httplib.py", line 417, in _read_status
raise BadStatusLine(line)
httplib.BadStatusLine: ''
以下是我正在尝试的代码:
url = 'https://torcache.net/torrent/7E74F1E4AEDC08A2D63BE1EDF612AC0BF17ECBBD.torrent?title=[kat.cr]batman.v.superman.dawn.of.justice.2016.720p.hdtc.1.2gb.shaanig'
torr_file = urllib2.urlopen(url)
with open("mytorrent.torrent", "w") as f:
f.write(torr_file.read())
我已经经历了几个类似的问题,但找不到解决方案。
您尝试下载的网站似乎放弃了 header 中没有 User-Agent
字符串的请求。我在这里找到了解决您问题的方法:https://whosebug.com/a/9265980/1577207.
添加 User-Agent
header 为我解决了这个问题:
import urllib2
url = 'https://torcache.net/torrent/7E74F1E4AEDC08A2D63BE1EDF612AC0BF17ECBBD.torrent?title=[kat.cr]batman.v.superman.dawn.of.justice.2016.720p.hdtc.1.2gb.shaanig'
opener = urllib2.build_opener()
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 5.1; rv:10.0.1) Gecko/20100101 Firefox/10.0.1',
}
opener.addheaders = headers.items()
torr_file = opener.open(url)
with open("mytorrent.torrent", "w") as f:
f.write(torr_file.read())