【发布时间】:2021-04-08 07:59:57
【问题描述】:
我正在尝试使用 requests 和 bs4 从 kali.org/downloads 快速下载 kali-linux live-amd64.iso.torrent 文件,但在 torrent 响应的标题中,没有:response.headers['content-disposition']My参考来自:How to download a file with .torrent extension from link with Python。我查看了 torrent_response 的标题,结果如下:
{'Server': 'nginx/1.14.2', 'Date': 'Thu, 08 Apr 2021 07:46:17 GMT', 'Content-Type': 'application/octet-stream', 'Content-Length': '274612', 'Connection': 'keep-alive', 'Last-Modified': 'Wed, 24 Feb 2021 17:39:18 GMT', 'ETag': '"60368f46-430b4"', 'X-Cache-Status': 'HIT', 'Accept-Ranges': 'bytes'}
class Download_Kali:
def __init__(self, locator):
self.locator = locator
def locate_torrent(self):
torrent_links = [torrent['href'] for torrent in self.locator.findAll('a', string='Torrent')]
for live_iso in torrent_links:
if 'live-amd64' in live_iso:
print('[*] Downloading iso Torrent')
return live_iso
def install(self):
import re, traceback
try:
with requests.get(Download_Kali(self.locator).locate_torrent()) as torrent_response:
torrent_response.raise_for_status()
disposition = torrent_response.headers
print(disposition)
#torrent_file = re.findall('filename="(.+)"', disposition)
#if torrent_file:
# with open(torrent_file[0], 'wb') as f_torrent:
# f_torrent.write(torrent_response.content)
except requests.HTTPError as err:
print(traceback.format_exc())
【问题讨论】:
-
为什么不直接使用
.iso图像?
标签: python python-3.x web-scraping