如何在通过分页(API)循环时附加几个字典-Python 3 [英] How to append several dictionaries while looping through pagination (API) - python 3

查看:75
本文介绍了如何在通过分页(API)循环时附加几个字典-Python 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想循环浏览通过API访问的几页数据,并在循环进行时将这些页面连接到同一词典.

I would like to loop through several pages of data which I am accessing through an API, and concatenate the pages to the same dictionary as the loop progresses.

到目前为止,我设法遍历了所有页面,并且可以在循环进行时打印出来,所以当循环结束时,所有页面都被打印出了.

So far I managed to loop through all the pages and could print out as the loop goes on, so when the loop is over, all the pages are printed out.

但是我的词典仅包含最后一页!

But my dictionary contains only the last page!

page = 5
i = 0
for i in range(0, page):
    url =f'http://hotell.difi.no/api/json/npd/wellbore/withcoordinates?page={i+1}'

    dataset_all = requests.get(url)
    json_dataset_all = dataset_all.json()
    json_dataset_all.update()
    print(json_dataset_all)
    i = i+1

我没有成功遍历循环并将字典page1更新为page2,page3,page4和page5.

I am not succeeding in going through the loop and updating my dictionary page1 with page2, page3, page4 and page5.

打印"表明可以循环浏览页面,但是随着过程的进行,它不会将页面存储在字典中.它仅包含最新页面.

The 'print' shows that the loop through the pages works, but it's not storing the pages in the dictionary as it progresses. It only contains the latest page.

预先感谢您的帮助.

推荐答案

页面上有一个相同的键",称为条目",其值是词典列表.因此,当您更新字典时,它只会覆盖其值(字典列表).

The pages have an identical "key" called "entries" and it's value is a list of dictionaries. When you update your dictionary, it will therefore just overwrite its value (the list of dictionaries).

如果您想要一个包含所有词典的列表,则可以执行以下操作.然后,您也可以轻松地将其放入Pandas DataFrame中.

If you want to have one single list with all the dictionaries, you could do something like this. You can then also easily put it in a Pandas DataFrame.

import requests
import pandas as pd

page = 5
i = 0
all_data_list = []
for i in range(0, page):
    url =f'http://hotell.difi.no/api/json/npd/wellbore/with-coordinates?page={i+1}'

    dataset_all = requests.get(url)
    dataset_all_json = dataset_all.json()
    number_of_entries = len(dataset_all_json['entries'])

    for x in range(0, number_of_entries):
        all_data_list.append(dataset_all_json['entries'][x])

    i = i+1

# Put all the results in a Pandas DataFrame
df = pd.DataFrame(all_data_list)
print(df)

(顺便说一句,您的URL缺少"withcoordinates"中的-")

(by the way, your URL is missing the '-' in "withcoordinates")

这篇关于如何在通过分页(API)循环时附加几个字典-Python 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆