discord.py와 함께 aiohttp를 사용하여 API 응답의 모든 페이지를 어떻게 읽을 수 있습니까?

LankyRyan

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")```
압둘라지즈

최대 페이지 수를 가져 와서 반복하면됩니다. 최종 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')

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관