API抓取器不工作/显示错误输出



所以我正在为一个游戏开发一个不和谐的.py机器人。该机器人旨在将拍卖的每个页面存储在一个列表中,然后我可以在每个页面中搜索特定的项目。我的代码是:

获取api列表:

apibaz = requests.get('https://api.hypixel.net/skyblock/bazaar?key=6c9b8b23-2d70-4b3d-a639-5276098b487a').json()
apiauc1 = requests.get('https://api.hypixel.net/skyblock/auctions?key=6c9b8b23-2d70-4b3d-a639-5276098b487a&page=1').json()
leg = (apiauc1['totalPages'])
leg = leg-1
apiaucM = []
v = 1
while v != (leg):
apiaucM.append(requests.get('https://api.hypixel.net/skyblock/auctions?key=6c9b8b23-2d70-4b3d-a639-5276098b487a&page=' + str(v)).json())
v = v+1

`

使用api查找输入的商品的价格:

if message.content.startswith('baz!bin'):
await message.channel.send('finding...')
y = 0
z = 0
listt = []
brii = message.content.split(None, 1)[1]
u = -1
p = -1
while p != (leg):
p = p+1
leng = len(apiaucM[0]['auctions'])
while u != (leng-1):
u = u+1
if brii == (apiaucM[p]['auctions'][u]['item_name']):
z = z+1
print('yues')
if 'bin' in (apiaucM[p]['auctions'][u]):
print('YES')
y = y+1
await message.channel.send('bin located')
cost = (apiaucM[p]['auctions'][u]['starting_bid'])
listt.append(cost)
print('b')
print('c')
x = 0
o = 0
lenp = len(listt)
if lenp == 1:
fcost = listt[0]
x = 1
for item in listt:
print(item)
if item > x:
print('dd')
x = 1
fcost = item



if z == 0:
await message.channel.send("I couldn't find any auctions for that item. Maybe check that the id is correct." )
elif x != 0:
await message.channel.send('The lowest BIN I could find was for ' + str(fcost) + ' coins. There were a total of ' + str(y) + ' BINs.')
else:
await message.channel.send('Sorry, I couldnt find any bins for that item.')

如果它只搜索一个页面,我认为它可能会这样做,那么它只会搜索拍卖行的1/40。输出为:

Discord输出

外壳输出

感谢您对为什么会发生这种情况提供任何帮助。

首先让我们清理您的代码并使其不阻塞。请求库不是异步的,因此是阻塞的。相反,您应该使用aiohttp库(https://docs.aiohttp.org/en/stable/client_reference.html)其是异步的。其次,如果你要发出多个请求,你应该发出一个Session,这样你就不会为每个请求启动一个新的Session。

from aiohttp import ClientSession

async with ClientSession() as session:  # Opens a session
async with session.get(firsturl) as page:  # gets a page within the session
apibaz = await page.json()  # assigns the json to the variable
async with session.get(secondurl) as page:
apiauc1 = await page.json()  # You should avoid using numbers in variables btw 
pages = apibaz['totalPages']
apiaucM = []
for i in range(1, pages):
async with session.get(f'https://api.hypixel.net/skyblock/auctions?key=6c9b8b23-2d70-4b3d-a639-5276098b487a&page={i}') as page:  # f strings are very useful, consider using them you can put a variable in a {} and it will be replaced by the string of the variable
apiaucM.append(await page.json())

对于代码的其余部分,您必须将其更改为更易于阅读。不要使用while循环,使用for循环和变量名应该描述它们的名称,而不仅仅是u、p、y、z。。。试着打印它从api中得到的内容,并检查它是否是你想要的。

相关内容

最新更新