【问题标题】:response.json was not working, now python is being funkyresponse.json 不工作,现在 python 很时髦
【发布时间】:2021-10-10 04:23:22
【问题描述】:

无论我尝试什么,JSON,作为一个混蛋,都无法正常工作。见代码:

import requests 
import response as response 
from pandas.io.formats import console class DataManager:     
# This class is responsible for talking to the Google Sheet.     
url = 'https://api.sheety.co/63978dbae6cae43f2cccf3e22fda65ce/myFlightDeals/prices';     
response = response.json     
pass

然后我尝试更新版本,创建了一个不同的问题。此问题:下午 5:28 安装包失败:安装包:发生错误。详情……

【问题讨论】:

  • 我发现这个代码示例有很多问题;我不知道从哪里开始。
  • 我也在 pypi 上搜索并找到了 this 库,但我认为它并没有达到你想要的效果。所以首先,我不明白import response 是从哪里来的。
  • rv.kvetch - 它来自我在 udemy 上的一门 Python 课程。教练让我把它放在那里,所以我做了,它在过去有效,所以我认为它会在这里有效。
  • 这些是说明。使用 Sheety API 发出请求 2. 现在使用 Sheety API 获取该工作表中的所有数据并将其打印出来。您应该会看到如下内容: 3. 尝试使用 from pprint import pprint 行导入漂亮的打印,然后使用 pprint() 再次打印数据以查看其格式。 4. 将“prices”键中存储的所有内容传回 main.py 文件,并将其存储在名为 sheet_data 的变量中,以便您可以从 main.py 打印 sheet_data

标签: python json api


【解决方案1】:

这是一种应该可行的方法。我仍然不清楚 import response 的用途,但是要向 url 发出 HTTP 请求并检索响应,您需要像这样使用 requests 库。请注意,如果您的端点支持,您也可以类似地基于 HTTP 动词发出其他请求,例如 GET、PUT、POST 和 HEAD。

import requests


url = 'https://api.sheety.co/63978dbae6cae43f2cccf3e22fda65ce/myFlightDeals/prices';

r = requests.get(url)

# in case you need to confirm request was a success (200 status)
# r.raise_for_status()

print(r.json())
# prints:
# {'errors': [{'detail': 'invalid_grant'}]}

# access error message, in case the request was invalid
err_msg: str = r.json()['errors'][0]['detail']

【讨论】:

  • 感谢您的帮助。根据您的方法,我能够使其正常工作
猜你喜欢
  • 1970-01-01
  • 2011-12-11
  • 2014-09-18
  • 1970-01-01
  • 1970-01-01
  • 2018-01-21
  • 2021-06-16
  • 2015-03-25
  • 1970-01-01
相关资源
最近更新 更多