0

从几个小时以来一直在尝试从我的 Pocket API 列表检索中获取数据帧。

但是我的代码最终只在一行中添加了所有内容,这不是我想要的。

我尝试了很多,但没有找到错误。

我的代码如下所示:

import json
import pandas as pd
from pandas.io.json import json_normalize
from flatten_json import flatten
import collections


import requests
from urllib.parse import urlencode
from urllib.request import Request, urlopen

# GET request for data in JSON format
parameters = {
     'consumer_key' : '9...9',
     'access_token' : 'c...b',
     'detailType' : 'complete',
     'count' : '10'
     }

response = requests.get('https://getpocket.com/v3/get', params=parameters)

json_string = response.json()
df=pd.json_normalize(json_string)

print(df)

结果如下所示:

status  complete error  ...     list.3483242700.domain_metadata.greyscale_logo list.3483242700.listen_duration_estimate search_meta.search_type
0       1         1  None  ...  https://logo.clearbit.com/washingtonpost.com?s...                                        0                  normal

源 JSON resonse.text 如下所示:

{"status":1,"complete":1,"list":{"237938806":{"item_id":"237938806","resolved_id":"237938806","given_url":"https:\/\/getpocket.com\/developer\/docs\/v3\/retrieve","given_title":"Pocket Developer Program: Pocket API: Retrieve","favorite":"0"

....

washpost.s3.amazonaws.com\/public\/SMWO67R4BAI6XKWZRFMSE4UAYQ.jpg&w=1440","domain_metadata":{"name":"The Washington Post","logo":"https:\/\/logo.clearbit.com\/washingtonpost.com?size=800","greyscale_logo":"https:\/\/logo.clearbit.com\/washingtonpost.com?size=800&greyscale=true"},"listen_duration_estimate":0}},"error":null,"search_meta":{"search_type":"normal"},"since":1638029019}
4

1 回答 1

0

试试df = pd.json_normalize(json_string["list"].values())

文档来看,如果你给它一个字典,它似乎pd.json_normalize会输出一行,但如果你给它一个字典列表,它会输出多行。由于来自 API 的 JSON 数据看起来像是将文章列表作为将项目 ID 映射到每个项目的字典返回,因此json_string["list"].values()将生成列表中所有文章的可迭代。

于 2021-11-27T17:26:46.843 回答