API json集合的嵌套while循环

2024-04-19 04:05:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我请求Meetup API提供590页。我使用while循环进行迭代以获取页面。现在我有了页面,我需要请求这些页面,并将它们正确地格式化为python,以便放置到一个数据帧中。你知道吗

This is how it looks when you do it for one url :

url = ('https://api.meetup.com/2/groups?offset=1&format=json&category_id=34&photo-host=public&page=100&radius=200.0&fields=&order=id&desc=false&sig_id=243750775&sig=768bcf78d9c73937fcf2f5d41fe6070424f8d0e3')
r = requests.get(url).json()
data = pd.io.json.json_normalize(r['results'])

But because I have so many pages I want to do this automatically and iterate through them all.

这就是嵌套while循环出现在脑海中的方式,这就是我尝试的:

urls = 0
offset = 0

url = 'https://api.meetup.com/2/groups?offset=%d&format=json&category_id=34&photo-host=public&page=100&radius=200.0&fields=&order=id&desc=false&sig_id=243750775&sig=768bcf78d9c73937fcf2f5d41fe6070424f8d0e3'

r = requests.get(urls%d = 'https://api.meetup.com/2/groups?offset=%d&format=json&category_id=34&photo-host=public&page=100&radius=200.0&fields=&order=id&desc=false&sig_id=243750775&sig=768bcf78d9c73937fcf2f5d41fe6070424f8d0e3').json()

while urlx < 591:
   new_url = r % urls % offset
   print(new_url)

   offset += 1  

但是,它不起作用,我收到许多错误,包括以下错误:

SyntaxError: keyword can't be an expression


Tags: httpscomapiidjsonformaturl页面
1条回答
网友
1楼 · 发布于 2024-04-19 04:05:55

不知道你想做什么,代码有很多问题。你知道吗

但是,如果您只想在0到591之间循环并获取URL,那么下面是代码:

import requests
import pandas as pd

dfs = []

base_url = 'https://api.meetup.com/2/groups?offset=%d&format=json&category_id=34&photo-host=public&page=100&radius=200.0&fields=&order=id&desc=false&sig_id=243750775&sig=768bcf78d9c73937fcf2f5d41fe6070424f8d0e3'

for i in range(0, 592):
    url = base_url % i
    r = requests.get(url).json()
    print("Fetching URL: %s\n" % url)

    # do something with r here
    # here I'll append it to a list of dfs

    dfs.append(pd.io.json.json_normalize(r['results']))

相关问题 更多 >