python - 如何使用 python 请求库从 Web 下载 PDF 文件

标签 python download python-requests

尝试使用 requests 模块从网站下载一些 pdf 文件,但我不断收到下面列出的此错误。我看到几个帖子,他们提到使用 response.content对于 pdf 文件而不是 response.text ,但它仍然会产生错误。不知道如何解决这个问题。
示例链接:https://corporate.exxonmobil.com/-/media/Global/Files/worldwide-giving/2018-Worldwide-Giving-Report.pdf

def scrape_website(link):
        
    try:
        print("getting content")
        cert = requests.certs.where()
        page = requests.get(link, verify=cert, headers={"User-Agent": "Mozilla/5.0 (X11; CrOS x86_64 12871.102.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.141 Safari/537.36"})
        
        print(page)
        if ".pdf" in link:
            print("the content is a pdf file. downloading..")
     

            return page.content
        
        return page.text

    except Exception as x:
        print(x)
        return ''

statement_page = scrape_website(link)


with open(filepath, 'w+', encoding="utf-8") as f: 
        print("writing page")
        f.write(statement_page)
        f.close()


    <ipython-input-42-1e4771d32073> in save_html_page(page, path, filename)
     13         with open(filepath, 'w+', encoding="utf-8") as f:
     14             print("writing page")
---> 15             f.write(page)
     16             f.close()
     17 

TypeError: write() argument must be str, not bytes

最佳答案

有时我也需要以编程方式下载东西。我只是用这个:

import requests

response = requests.get("https://link_to_thing.pdf")
file = open("myfile.pdf", "wb")
file.write(response.content)
file.close()
您也可以使用 os使用 wget 下载的包:
import os

url = 'https://link_to_pdf.pdf'
name = 'myfile.pdf'

os.system('wget {} -O {}'.format(url,name))

关于python - 如何使用 python 请求库从 Web 下载 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64178665/

相关文章:

python - 在Python中,如何使用Locust和Requests包?

python - 如何使用 requests、Python 和 Firebase 添加数据而不是替换数据

Python请求收到404,wget获取正确页面

python - 如何将 "google.cloud.documentai_v1.types.document"对象转换为 json

python - fasta 文件标题行插入列

ruby-on-rails - 从 Heroku 上的 Rails 下载流式 ZIP 文件导致内存使用量增加

javascript - 使用 JS 和 html5 从字符串创建文本文件

python - 如何使用 python pandas 在循环中加入多个数据帧

python - 嵌套/多个列表理解或生成器表达式的用例。什么时候更优雅?

image - golang api 图像下载中的图像文件损坏