【问题标题】:Download PDF from PeerJ从 PeerJ 下载 PDF
【发布时间】:2022-01-15 00:58:35
【问题描述】:

我正在尝试使用 Python requestsPeerJ 下载 PDF。例如,https://peerj.com/articles/1.pdf

我的代码很简单:

r = requests.get('https://peerj.com/articles/1.pdf')

但是,返回的 Response 对象显示为 <Response [432]>,这表示 HTTP 432 错误。据我所知,该错误代码未分配。

当我检查 r.textr.content 时,有一些 HTML 表明这是一个错误 432,并提供了指向同一 PDF 的链接,https://peerj.com/articles/1.pdf

当我在浏览器 (Chrome) 中打开 PDF 时,我可以查看它。

如何获取实际的 PDF(作为 bytes 对象,就像我应该从 r.content 获取一样)?

【问题讨论】:

    标签: python pdf python-requests


    【解决方案1】:

    在打开网站时,您提到过,我还在我的 Firefox 浏览器中打开了开发人员工具,并从那里复制了 http 请求标头并将其分配给 request.get 函数中的 headers 参数。

    a = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,/;q=0.8', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'en-US,en;q=0.5', 'Connection': 'keep-alive', 'Host': 'peerj.com', 'Referer': 'https://peerj.com/articles/1.pdf', 'Sec-Fetch-Dest': 'document', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-Site': 'same-origin', 'Sec-Fetch-User': '?1', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0'}

    r = requests.get('https://peerj.com/articles/1.pdf', headers= a)

    【讨论】:

    • 甚至可以使用 a = { 'Referer': '<a href="/default/index/tourl?u=aHR0cHM6Ly9wZWVyai5jb20vYXJ0aWNsZXMvMS5wZGY%3D" rel="nofollow" target="_blank">peerj.com/articles/1.pdf</a>'} r = requests.get('<a href="/default/index/tourl?u=aHR0cHM6Ly9wZWVyai5jb20vYXJ0aWNsZXMvMS5wZGY%3D" rel="nofollow" target="_blank">peerj.com/articles/1.pdf</a>', headers= a)跨度>
    猜你喜欢
    • 1970-01-01
    • 2021-01-09
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多