【发布时间】:2020-07-09 01:39:52
【问题描述】:
需要一些专家的帮助来解决这个问题并在缺失的地方强制使用双引号,这样我才能得到成功的响应。它在这里抛出错误 df = pd.io.json.json_normalize(rt.json(), record_path='offers')。 josn 看起来不错,我已经上演到 csv,所以不确定问题出在哪里
我的代码
import requests
import csv
import json
import pandas as pd
from pandas.io.json import json_normalize
url = "https://authentication.skiapi.com/access_token"
payload = {
"client_id": "00c7fcf******",
"client_secret": "7676cd5a********",
"grant_type": "client_credentials"
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
r = response.json()
access_token = r.get('access_token')
print(response.json()['access_token'])
uri = "https://private-anon-73f9ac5d87-slinksmerchantapi.apiary-mock.com/v4/publisher/12633/offers?country=US"
headers = {'Authorization': access_token,'Content-Type': "application/json"}
rt = requests.get(uri, headers=headers)
df = pd.io.json.json_normalize(rt.json(), record_path='offers') ###problem is here
#print(df)
df.to_csv(r"C:\\Users\ral\Downloads\\offers.csv", index=False)
我的 JSON 示例
{
"has_more": true
"offers": [{
"coupon_code": null
"terms": null
"description": "40% Off Comforters & Other Cool Bedding Stuff Online at BoxLunch! Stock up on select blankets and bedding online only for a limited time -- See site for details. Valid 3/3-3/5"
"offer_starts": "2017-03-03 08:00:00"
"title": "40% Off Comforters & Other Cool Bedding Stuff Online at BoxLunch!"
"url": "http://www.boxlunch.com/home/bedroom/?soffer=152034"
"merchant_details": {
"domain": "boxlunchgifts.com"
"verticals": []
"country_code": null
"id": 393756
"metadata": {}
"favourite": false
"partner_type": null
"merchant_id": 383288
"advertiser_id": 123456
"name": "BoxLunch"
"countries": []
"domains": [
"boxlunchgifts.com"
"boxlunch.com"
]
}
"offer_type": "sale"
"id": 152034
"offer_ends": "2017-03-05 08:00:00"
}]
"last_val": 152034
"next_val": 152032
"num_returned": 1
}
【问题讨论】:
-
@tdelaney ..没办法..我确实擦洗了它们..但是我对帖子进行了编辑以避免这种混乱谢谢
-
这个示例 json 是在哪里形成的?这可能是我自己缺乏知识,但我注意到的第一件事是没有逗号分隔元素。我认为这样的事情会导致 JSON 解码器出现问题。更多关于该示例 json 的上下文可能会有所帮助。
-
@Eddie 这是一个 API 调用,响应是 JSON。我们可以在这里使用 JSON 格式化函数吗?确保它是正确的 JSON 格式
-
json 无效。至少缺少逗号。你可以
json.dump(rt.json(), open("rt.json", "w"), indent=4)然后发布。此外,确切的 python 回溯很有用,因此我们可以看到失败的位置。 -
想一想,如果json解码失败,
rt.content或rt.raw.read()可能会更好。
标签: python pandas jupyter-notebook