Python 如何下载没有[';内容处置';]的.torrent文件?

Python 如何下载没有[';内容处置';]的.torrent文件?,python,python-3.x,web-scraping,Python,Python 3.x,Web Scraping,我正在尝试使用请求和bs4从kali.org/downloads快速下载kali linux live-amd64.iso.torrent文件,但在torrent响应的标题中没有:response.headers['content-disposition']我的引用来自:。我查看了torrent_响应的标题,结果如下: {'Server':'nginx/1.14.2','Date':'Thu,08 Apr 2021 07:46:17 GMT','Content Type':'applicatio

我正在尝试使用请求和bs4从kali.org/downloads快速下载kali linux live-amd64.iso.torrent文件,但在torrent响应的标题中没有:
response.headers['content-disposition']
我的引用来自:。我查看了torrent_响应的标题,结果如下:

{'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,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
图像,这里是我的看法。我使用的是
netinstaller
,因为它体积更轻。此外,我还添加了一个进度条

导入shutil
导入请求
从bs4导入BeautifulSoup
从TQM导入TQM
将requests.Session()作为连接:
kali_page=connection.get(“https://kali.org/downloads/1.内容
iso_图像=[
t[“href”]表示输入的t
BeautifulSoup(kali_页面,“lxml”)。选择(“表a”)
如果不是t[“href”].endswith(“.torrent”)
]
对于iso_图像中的iso_图像:
如果iso_图像中出现“netinst-arm64”:
打印(f“下载{iso_图像}”)
文件名=iso\u image.rsplit(“/”[-1]
以connection.get(iso_image,stream=True)作为响应\
打开(文件名,“wb”)作为输出:
total_size=int(response.headers.get('content-length',0))
使用TQM(
总数=总尺寸/(32*1024.0),
第二单元,
单位刻度=真,
单位除数=1024,
)作为进度条:
对于响应中的数据。iter_内容(32*1024):
进度条更新(透镜(数据))
shutil.copyfileobj(response.raw,output)
这应该显示如下内容:


为什么不直接使用
.iso
图像?谢谢,效果很好。虽然这对我来说是新的,对我来说学习它也会很好。