如何在Python中的嵌套json中使用Meta

cbwuti44  于 2023-10-21  发布在  Python
关注(0)|答案(1)|浏览(115)

我需要在JSON URL中查找信息。我使用以下代码打开URL:

link = 'https://cordis.europa.eu/search/fr?q=contenttype%3D%27project%27%20AND%20programme%2Fcode%3D%27H2020%27&p=1&num=10&srt=/project/contentUpdateDate:decreasing&format=json'
r = requests.get(link)

这是第一个嵌套的beeing:

pd.json_normalize(r.json(),record_path=['hits','hit'])

这一个beeing在我需要的水平:

pd.json_normalize(r.json(),record_path=['hits','hit','project','relations','associations','organization'])

直到这里,它的确定(也许有一些简单的,但它的确定)
困难是我所知道的。我不完全理解“Meta”方法。我不想从最高层得到身份证:项目ID
我试过了,但是有一个错误:

pd.json_normalize(r.json(),record_path=['hits','hit','project','relations','associations','organization'], meta = ['project.id'])

他找不到名称“project.id”。你有身份证吗

3lxsmp7m

3lxsmp7m1#

这能解决你的问题吗?

import requests
import pandas as pd

def get_project_ids():
    link = 'https://cordis.europa.eu/search/fr?q=contenttype%3D%27project%27%20AND%20programme%2Fcode%3D%27H2020%27&p=1&num=10&srt=/project/contentUpdateDate:decreasing&format=json'
    r = requests.get(link)

    df = pd.json_normalize(r.json(), record_path=['hits', 'hit', 'project', 'relations', 'associations', 'organization'])
    return df['id'].tolist()
    
print(get_project_ids())

相关问题