我使用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
答案 0 :(得分:15)
我相信所有现有的答案都包含相关信息,但我想总结一下。
requests
get 和 post 操作返回的响应对象包含两个有用的属性:
response.text
- 包含带有响应文本的 str
。response.content
- 包含带有 原始 响应内容的 bytes
。您应该根据您期望的响应类型选择这些属性中的一个或其他。
response.text
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)
原始代码应该使用 wb
和 response.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()
但我会更进一步,使用 with
context 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)
答案 1 :(得分:6)
彼得已经指出:
r.text
您可能还想查看#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int scmpr (const void *a, const void *b){//receive char **
return strcmp(*(const char**)a, *(const char**)b);
}
int ccmpr (const void *a, const void *b){//compare one char
unsigned char x = *(unsigned char *)a;
unsigned char y = *(unsigned char *)b;
return (x > y) - (x < y);
}
int main(void){
int i,j;
char **tab;
tab=(char**)malloc(sizeof(char*)* 10);
for(i=0; i<10; i++){
tab[i]=(char*)malloc(sizeof(char)*16);//+1 for NUL char to use strcmp
}
for(i=0; i<10; i++){
for(j=0; j<15; j++){
tab[i][j]=rand()%20+'b';
printf("%c ", tab[i][j]);
}
tab[i][j] = 0;//set NUL
puts("");
}
for (i = 0; i<10; i++){
qsort(&tab[i][0], 15, sizeof(char), ccmpr);
}
qsort(tab, 10, sizeof(char*), scmpr);//element is char*
puts("");
for(i=0; i<10; i++){
for(j=0; j<15; j++){
printf("%c ", tab[i][j]);
}
puts("");
}
//deallocate
return 0;
}
。
另外:http://docs.python-requests.org/en/latest/user/quickstart/
答案 2 :(得分:5)
您可以使用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
file = open("resp_text.txt", "w")
file.write(response.text)
file.close()
file = open("resp_content.txt", "w")
file.write(response.text)
file.close()