【问题标题】:How can I read all pages of an API response using aiohttp with discord.py?如何使用 aiohttp 和 discord.py 读取 API 响应的所有页面?
【发布时间】:2020-11-30 21:55:05
【问题描述】:

我正在使用 discord.py 重写和 aiohttp。这个 API 的文档很少,据我所见,我在响应中的任何地方都没有看到“next_page”链接。

我该如何做到,以便在执行查询时考虑 json 响应的所有页面,而不仅仅是默认的第一页?

这是我当前的相关代码:

async def command(ctx, *, name):
    if not name:
        return await ctx.channel.send('Uhhhh. How am I supposed to show you info if you don\'t enter a name?')
    async with ctx.channel.typing():
        async with aiohttp.ClientSession() as cs:
            async with cs.get("https://website.items.json") as r:
                data = await r.json()
                listings = data["items"]
                for k in listings:
                    if name.lower() == k["name"].lower():
                        await ctx.channel.send("message with results and player info as ascertained in the above code")```

【问题讨论】:

    标签: python python-3.x pagination discord.py aiohttp


    【解决方案1】:

    您只需获取最大页数并对其进行迭代。最终的 url 应该是这样的。

    https://theshownation.com/mlb20/apis/items.json?page=2
    

    这是你如何设置循环

    for page_number in range(1,total_pages):
        url = f'https://theshownation.com/mlb20/apis/items.json?page={page_number}'
    

    编辑:

    完整的实现是这样的。

    @bot.command()
    async def card(ctx, *, player_name):
        if not player_name:
            return await ctx.channel.send('Uhhhh. How am I supposed to show you player info if you don\'t enter a player name?')
    
        async with ctx.channel.typing():
            async with aiohttp.ClientSession() as cs:
                for page_number in range(1, 20):
                    async with cs.get(f"https://theshownation.com/mlb20/apis/items.json?page={page_number}") as r:
                        data = await r.json()
                        print(data["page"])
                        listings = data["items"]
                        for k in listings:
                            if player_name.lower() == k["name"].lower():
                                return await ctx.channel.send("message with results and player info as ascertained in the above code")
                                
    
        await ctx.send('Sorry could not find him')
    

    【讨论】:

    • 感谢您的回复。我已经做了几个变体,但我一定做错了。使用您的示例``` for page_number in range(1,122): async with cs.get("theshownation.com/mlb20/apis/items.json&page=" + {page_number}) as r:``` 给我这个回溯: TypeError: can only concatenate str (not "设置") 到 str
    • 应该是async with cs.get(f"https://theshownation.com/mlb20/apis/items.json&page={page_number}") as r
    • 好的,我刚刚插入了那个建议。不再有回溯错误,但它仍然只是从 API 响应的第一页中提取。例如,当键入第 77 页上的名称时,控制台将重复打印 API 响应的第一页。当在 API 响应的第一页上键入名称时,控制台会在重复时打印 API 响应的第一页,并且机器人会在重复时将我的 await ctx.channel.send 消息发送到不和谐通道。有什么建议吗?
    • 我看到您没有正确执行循环。我在上面进行了编辑以向您展示如何操作。请记住,您需要对其进行一些更改以使其循环为total_pages
    • 谢谢,我想它越来越近了。将return 语句添加到发送消息中可以阻止机器人发送数百条消息,尽管现在它只发送一个匹配项,而可以有多个消息(相同的player_name)。我尝试了几个不同版本的循环来尝试读取 API 响应的所有页面,但我似乎仍然无法让它超过第 1 页。我很抱歉,我一直很难找到适用的示例使用 aiohttp 而不是请求,当我有时,我无法让它们为此目的工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 2020-11-24
    相关资源
    最近更新 更多