日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python request下载-Python使用requests下载文件问题

發(fā)布時間:2025/3/20 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python request下载-Python使用requests下载文件问题 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

最近在爬一個網(wǎng)站,想直接下載其中的torrent文件,發(fā)現(xiàn)該torrent文件在下載頁面是點擊下載按鈕,提交一個form表單到后臺,然后開始下載,使用python requests提交表單,但是下載下來的文件是論壇的首頁,不知道哪里出錯了,有大佬幫看看嗎?以下是代碼,老司機懂的.

def down_torrent(link):

headers = {

"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"

"Accept-Encoding": "gzip, deflate"

"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8"

"Cache-Control": "max-age=0"

"Connection": "keep-alive"

"Host": "www3.uptorrentfilespacedownhostabc.info"

"Upgrade-Insecure-Requests": "1"

"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.360"

}

torrent_html = requests.get(link)

torrent_html.encoding == "utf-8"

soup = BeautifulSoup(torrent_html.text, "lxml")

input_list = soup.find_all("input")

down_parms = {} #POST提交的參數(shù)

for i in input_list:

if i.get("name"):

down_parms[i.get("name")] = i.get("value")

#POST地址

post_link = link.split("file.php")[0] + "down.php"

#下載文件保存位置

down_dir = os.path.join(current_path, down_parms["name"])

if not os.path.exists(down_dir):

os.mkdir(down_dir)

down_path = os.path.join(down_dir, down_parms["name"])

down_path = down_path + ".torrent"

s = requests.Session()

torrent_html = s.post(post_link, headers=headers, data=down_parms)

with open(down_path, "wb") as f:

for chunk in torrent_html.iter_content(10):

f.write(chunk)

if __name__ == "__main__":

down_torrent("http://www3.uptorrentfilespacedownhostabc.info/updowm/file.php/P22OGZq.html")

總結(jié)

以上是生活随笔為你收集整理的python request下载-Python使用requests下载文件问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。