【问题标题】:Saving response from Requests to file将请求的响应保存到文件
【发布时间】:2015-09-16 13:27:27
【问题描述】:

我正在使用 Requests 将 PDF 上传到 API。它在下面存储为“响应”。我正在尝试将其写入 Excel。

import requests

files = {'f': ('1.pdf', open('1.pdf', 'rb'))}
response = requests.post("https://pdftables.com/api?&format=xlsx-single",files=files)
response.raise_for_status() # ensure we notice bad responses
file = open("out.xls", "w")
file.write(response)
file.close()

我收到了错误:

file.write(response)
TypeError: expected a character buffer object

【问题讨论】:

  • with open(filename, mode='wb') as localfile:     localfile.write(response.content) 比打开和关闭更干净优雅,恕我直言。
  • 跳过前两个答案,直接前往this one。如果 OP 可以将复选标记移至该答案,那就太好了——现有的最高答案几乎没有用,而最受好评的答案的用处有限。

标签: python file-io python-requests


【解决方案1】:

正如Peter 已经指出的那样:

In [1]: import requests

In [2]: r = requests.get('https://api.github.com/events')

In [3]: type(r)
Out[3]: requests.models.Response

In [4]: type(r.content)
Out[4]: str

您可能还想查看r.text

还有:https://2.python-requests.org/en/latest/user/quickstart/

【讨论】:

  • 我认为您没有回答 OP 的问题。如何将其保存在文件中。你所做的只是描述r
【解决方案2】:

您可以使用response.text 写入文件:

    import requests
    
    files = {'f': ('1.pdf', open('1.pdf', 'rb'))}
    response = requests.post("https://pdftables.com/api?&format=xlsx-single",files=files)
    response.raise_for_status() # ensure we notice bad responses
    with open("resp_text.txt", "w") as file:
        file.write(response.text)

【讨论】:

  • 或使用with open(...): ...
【解决方案3】:

我相信所有现有的答案都包含相关信息,但我想总结一下。

requests get 和 post 操作返回的响应对象包含两个有用的属性:

响应属性

  • response.text - 包含 str 和响应文本。
  • response.content - 包含带有原始响应内容的bytes

您应该根据您期望的响应类型选择这些属性中的一个或其他。

  • 对于基于文本的响应(html、json、yaml 等),您可以使用response.text
  • 对于基于二进制的响应(jpg、png、zip、xls 等),您可以使用 response.content

写响应文件

向文件写入响应时,您需要使用open function 和适当的文件写入模式。

  • 对于文本回复,您需要使用"w" - 纯写模式。
  • 对于二进制响应,您需要使用"wb" - 二进制写入模式。

示例

文本请求并保存

# Request the HTML for this web page:
response = requests.get("https://stackoverflow.com/questions/31126596/saving-response-from-requests-to-file")
with open("response.txt", "w") as f:
    f.write(response.text)

二进制请求并保存

# Request the profile picture of the OP:
response = requests.get("https://i.stack.imgur.com/iysmF.jpg?s=32&g=1")
with open("response.jpg", "wb") as f:
    f.write(response.content)

回答原问题

原始代码应该可以使用wbresponse.content

import requests

files = {'f': ('1.pdf', open('1.pdf', 'rb'))}
response = requests.post("https://pdftables.com/api?&format=xlsx-single",files=files)
response.raise_for_status() # ensure we notice bad responses
file = open("out.xls", "wb")
file.write(response.content)
file.close()

但我会更进一步,使用withcontext manager for open

import requests

with open('1.pdf', 'rb') as file:
    files = {'f': ('1.pdf', file)}
    response = requests.post("https://pdftables.com/api?&format=xlsx-single",files=files)

response.raise_for_status() # ensure we notice bad responses

with open("out.xls", "wb") as file:
    file.write(response.content)

【讨论】:

  • 如何找出你得到的文件类型?例如,我正在从用户给我的链接下载一个文件,我必须找出我需要做什么类型的写作。
  • 这是个好问题。查看stackoverflow.com/a/898723/6252525 这建议使用mimetypes.guess_type(),然后使用查找来确定mime 类型是否为二进制。
猜你喜欢
  • 2012-12-13
  • 2016-08-04
  • 1970-01-01
  • 1970-01-01
  • 2021-09-08
  • 2018-12-09
  • 2023-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多