如何在使用python登录站点后触发.txt文件的下载

2024-05-03 05:47:05 发布

您现在位置:Python中文网/ 问答频道 /正文

用于登录的代码:

import requests
from bs4 import BeautifulSoup as bs
import urllib

payload = {       
   'email'    : 'xxx@gmail.com',
   'password' : 'xxx'
}

with requests.Session() as s:
    m  = s.get('https://www.free-ebooks.net',headers={'User-agent': 'Mozilla/5.0'})   
    t  = s.post('https://www.free-ebooks.net',data = payload)
    r  = s.get('https://www.free-ebooks.net/ebook/The-Best-Scandal-Ever')
    print r.content

根据print r.content的输出,我认为我的登录是成功的
用于触发下载的代码:

^{pr2}$

在我的输出pdf中,我得到的是源代码,而不是pdf的原始内容。
我觉得我应该使用已经开始的实例会议。但是不知道如何实施。
任何一个?在


Tags: 代码httpsimportfreegetnetpdfas
1条回答
网友
1楼 · 发布于 2024-05-03 05:47:05

requests和{}不同。他们不共享信息(尤其是cookie)。在

始终使用requests'。在

with requests.Session() as s:
    m  = s.get('https://www.free-ebooks.net', headers={'User-agent': 'Mozilla/5.0'})
    t  = s.post('https://www.free-ebooks.net', data=payload)
    r  = s.get('https://www.free-ebooks.net/ebook/The-Best-Scandal-Ever')
    resp = s.get("https://www.free-ebooks.net/ebook/The-Best-Scandal-Ever/txt",
                 stream=True)
    with open("myfile007.pdf", "wb") as f:
        f.writelines(resp.iter_content())

相关问题 更多 >