运行HTMLrequests()时出现Tracemalloc错误

2024-10-01 09:33:05 发布

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

我正在写一个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

Tags: infromimportselfurlmessagegethtml
1条回答
网友
1楼 · 发布于 2024-10-01 09:33:05

此错误通常是由于未使用异步函数引起的。在本例中,我认为这是在调用get_url()时引起的。我假设您创建该嵌入函数是因为它要求您等待asession.get,而您不能,因为get_sizes_prices也不是异步的

下面是一些未经测试的代码,应该解决您的问题

async def get_sizes_prices(self):
    nest_asyncio.apply()
    asession = AsyncHTMLSession()

    # create session from given endpoint with keywords/sku
    response = await asession.get('a special URL' + self._url)

    result_slug = response.html

我所做的是删除了嵌入的函数。将await语句添加到asession.get并使整个函数异步。因此,您还需要await这个函数,无论您在哪里调用它

我使用文档here作为参考,说明哪些方法需要等待或不需要等待。希望能解决你的问题:)

相关问题 更多 >