我正在写一个Discord机器人,它发送一个网络钩子,包括从网站上抓取的信息
刮取信息的功能如下:
import json
from requests_html import AsyncHTMLSession
import requests_html
import nest_asyncio
from bs4 import BeautifulSoup
class Product:
def __init__(self, url):
self._url = url
def get_sizes_prices(self):
nest_asyncio.apply()
asession = AsyncHTMLSession()
# create session from given endpoint with keywords/sku
async def get_url():
slug = asession.get('a special URL' + self._url)
result = asession.run(get_url())
result_slug = result.html
**Some more code down there which gets some special things from the scraped website**
return **variables**
运行此操作时,我收到以下错误:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\lucab\PycharmProjects\untitled\venv\lib\site-packages\discord\client.py", line 333, in _run_event
await coro(*args, **kwargs)
File "C:/Users/lucab/OneDrive/Bots_-Coding/NexusTools/restocksscraper/main.py", line 15, in on_message
array = product_obj.get_sizes_prices()
File "C:\Users\lucab\OneDrive\Bots_-Coding\NexusTools\restocksscraper\get_product.py", line 21, in get_sizes_prices
results = asession.run(getlink())
File "C:\Users\lucab\PycharmProjects\untitled\venv\lib\site-packages\requests_html.py", line 772, in run
asyncio.ensure_future(coro()) for coro in coros
File "C:\Users\lucab\PycharmProjects\untitled\venv\lib\site-packages\requests_html.py", line 772, in <listcomp>
asyncio.ensure_future(coro()) for coro in coros
TypeError: 'coroutine' object is not callable
C:\Users\lucab\PycharmProjects\untitled\venv\lib\site-packages\discord\client.py:340: RuntimeWarning: coroutine 'Product.get_sizes_prices.<locals>.getlink' was never awaited
pass
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
首先,我只使用了HTMLSession(),在检查了文档之后,我使用了asynchtmlsesession,bc这是请求推荐的_html(https://pypi.org/project/requests-html/)
还有我的Discord机器人代码:
import discord , os
from dotenv import load_dotenv
from get_product import Product
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
client = discord.Client()
@client.event
async def on_message(message):
if message.content.startswith('!test'):
product = message.content
product.replace('!test', '') #to only get the string after the !test command
product_obj = Product(product)
array = product_obj.get_sizes_prices() #the return values are stored in "array"
varembed = discord.Embed(
title = "XXXXXX",
description='SKU: '+XXXXX,
url = XXXXXX,
color = discord.Colour.darker_grey(),
)
varembed.set_footer(text="XXXX" )
for i in array[0]:
for x in array[1]:
varembed.add_field(name="",value="", inline=True)
await message.channel.send(embed=varembed)
client.run(TOKEN)
编辑: 对我起作用的是: 使用等待调用Discord Bot中的函数:
array = await product_obj.get_sizes_prices()
将异步设置为函数本身:
async def get_sizes_prices(self):
并将异步_会话设置为等待:
result = await async_session.get('some url' + self._url)
result_slug = result.text
此错误通常是由于未使用异步函数引起的。在本例中,我认为这是在调用
get_url()
时引起的。我假设您创建该嵌入函数是因为它要求您等待asession.get
,而您不能,因为get_sizes_prices
也不是异步的下面是一些未经测试的代码,应该解决您的问题
我所做的是删除了嵌入的函数。将
await
语句添加到asession.get
并使整个函数异步。因此,您还需要await
这个函数,无论您在哪里调用它我使用文档here作为参考,说明哪些方法需要等待或不需要等待。希望能解决你的问题:)
相关问题 更多 >
编程相关推荐