将响应从请求保存到文件 [英] Saving response from Requests to file

查看:111
本文介绍了将响应从请求保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用请求将PDF上传到API.它存储为下面的响应".我正在尝试将其写到Excel.

I'm using Requests to upload a PDF to an API. It is stored as "response" below. I'm trying to write that out to 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

推荐答案

彼得已经指出:

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/zh/latest/user/quickstart/

这篇关于将响应从请求保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆