【发布时间】:2022-01-11 13:39:29
【问题描述】:
已编辑:
这是我的步骤:
url = "https://ted.europa.eu/api/v2.0/notices/search?&q=TD%3D%5B3%5D&reverseOrder=true&scope=3&sortField=PD"
# get data from url
response = requests.get(url)
# return the json data, and read the output dict keys
data = response.json()
data
我已经从一个 api 获得了一个 json 文件:
{'took': 205, total': 1703997, 'results': [{'AA': '1', 'AC': '2', 'BI': [], 'CY': 'MK', 'DI': '1046/2018', 'TY': '1'}, {'AA': '6', 'AC': '1', 'BI': [], 'CY': 'RS', 'DI': 'CODE_OTHERS', 'TY': '1'}, {'AA': '5', 'AC': '1', 'BI': [], 'CY': 'BE', 'DI': '1046/2018', 'TY': '1'}, ...
#read the output dict keys
data.keys()
当我将其转换为 pd df
df = pd.DataFrame(data["results"])
dict_keys(['took', 'total', 'results'])
# create dataframe from key of interest
df = pd.DataFrame(data["results"])
df.head()
这按预期返回了数据帧...
# count number of rows
len(df.index)
1000
但是,我预计总共是 1703997。
我还在思考如何解决这个问题????
知道我该怎么做吗?
【问题讨论】:
-
您的 API 应该能够将页码作为参数。在while循环中,存储页面
n的100个结果并递增n直到返回的json为空。 (或循环for n in range(result['total']//100)) -
嗨 Tranbi,你能更详细地介绍一下 while 循环吗?谢谢
-
您应该首先描述如何获取这些数据。对下一页重复该过程,直到结果为空。
-
嗨,我在最近的编辑中详细介绍了我的步骤。请看一看。
标签: python json pandas api pyspark