python request下载-Python使用requests下载文件问题
最近在爬一個網站,想直接下載其中的torrent文件,發現該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提交的參數
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")
總結
以上是生活随笔為你收集整理的python request下载-Python使用requests下载文件问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为什么一个程序申请的内存有限制_为什么要
- 下一篇: docker image aarch64