python - 通过 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。
有人能帮我一下吗?
谢谢!
解决方案
我用身份验证修复了它
#!/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。但脚本当然还有更多输出。
推荐阅读
- c++ - 变量地址的运算符[]如何工作?
- python - 用符号序列替换字符串中的关键字
- python - setup.py 脚本无法识别模块
- intershop - 从 ISML 渲染 PDF 并检索二进制数据
- python - 您使用哪个版本的 fbs、PySide2、shiboken2 和 python 来分发 Qt for Python 项目?
- c# - ASP.NET 4.0 图表控件损坏的图像链接
- microsoft-graph-api - Microsoft Graph API Beta - 获取聊天返回 401 Unauthorized
- apache2 - VirtualHost Apache2 似乎被忽略了——尽管 DNS 传播,只有我的本地主机文件才能正常工作
- html - 无法让文字出现在图像下方
- javascript - 将属性添加到在 jQuery 中,每个循环仅在第一次通过时?