首页 > 解决方案 > 通过 Python 导出到 csv 从 API 获取请求

问题描述

我正在尝试通过 python 编写一个 api 脚本。我想通过“requests.get”访问 URL“https://xxx/2.0/article”,以便显示。如果这成功了,我想重新编程以将所有“文章”写入 CSV。

我首先使用 curl 并且效果很好

curl -X GET \
https://xxx/2.0/contact \
-H 'Accept: application/json' \
-H 'Authorization: Bearer {access-token}'

它还直接显示数据。当我用 Python 尝试这个时,它给了我一个“未经授权”。

 import requests
 newHeaders = {'Content-type': 'application/json', 'Accept': 'text/plain'}
 response = requests.get('https://api.bexio.com/2.0/article', headers={'Authorization': 'newHeaders' 
 'Bearer '})
 print(response.status_code)
 print(response.text)

我不确切知道我必须做什么才能完成这项工作,以及如何将其直接导出/写入 CSV。

有人能帮我一下吗?

谢谢!

标签: pythonjsoncsvrequesttoken

解决方案


我用身份验证修复了它

#!/usr/bin/python
import requests

url = "https://api.bexio.com/2.0/article"

headers = {
'Accept': "application/json",
'Content-Type': "application/json",
'Authorization': "Bearer }

response = requests.request("GET", url, headers=headers)

print(response.text)

我看到了我的错。最后一个问题是,如何获取 csv 文件中的输出?当我在前端导出我的库存时,我得到一个包含“产品”“代码”“存储位置”“购买价值”“库存”和“最低库存”字段的 CSV。但脚本当然还有更多输出。


推荐阅读