函数和异步有问题
having trouble with function and asyncio
到目前为止,这是我的代码:
import discord
from discord import Webhook, AsyncWebhookAdapter
from discord.ext import commands
from discord import Activity, ActivityType
import aiohttp
from bs4 import BeautifulSoup
from requests_html import AsyncHTMLSession
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix="$", intents=intents, case_insensitive=True)
async def amazon():
URL = "https://www.amazon.com/s?k=gaming"
with AsyncHTMLSession() as session:
response = await session.get(URL)
response.html.arender(timeout=20)
soup = BeautifulSoup(response.html.html, "lxml")
results = soup.select("a.a-size-base.a-link-normal.s-link-style.a-text-normal")
max_price = 10
for result in results:
price = result.text.split('$')[1].replace(",", "")
if float(price) < max_price:
print(f"Price: ${price}\nLink: https://www.amazon.com{result['href'].split('?')[0]}")
@client.command()
async def amaz(ctx):
await amazon()
await ctx.send("hello")
client.run("iputmytokenhere")
这是我在执行 $amaz 时遇到的错误:
RuntimeWarning: coroutine 'HTML.arender' was never awaited
response.html.arender(timeout=20)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
C:\Users\CK\AppData\Roaming\Python\Python37\site-packages\requests\sessions.py:428: RuntimeWarning: coroutine 'AsyncHTMLSession.close' was never awaited
self.close()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
我将其用作一个有趣的项目,非常感谢任何帮助。我尝试了很多东西,但似乎没有任何效果。我希望机器人将抓取的数据发送到 discord webhook。
+您可以使用以下代码实现amazon()
功能。
async def amazon():
URL = "https://www.amazon.com/s?k=gaming"
# add the string of the webhook url
WEBHOOK_URL = "https://your_webhook_url"
# change the with statement to assignment following the documentation
session = AsyncHTMLSession()
response = await session.get(URL)
# add await to prevent the "was not awaited" error
await response.html.arender(timeout=20)
# create the webhook object
webhook = Webhook.from_url(WEBHOOK_URL, adapter=AsyncWebhookAdapter(session))
soup = BeautifulSoup(response.html.html, "lxml")
results = soup.select("a.a-size-base.a-link-normal.s-link-style.a-text-normal")
max_price = 10
for result in results:
price = result.text.split('$')[1].replace(",", "")
if float(price) < max_price:
# change print() to webhook.send() to send the data from a webhook
await webhook.send(f"Price: ${price}\nLink: https://www.amazon.com{result['href'].split('?')[0]}")
到目前为止,这是我的代码:
import discord
from discord import Webhook, AsyncWebhookAdapter
from discord.ext import commands
from discord import Activity, ActivityType
import aiohttp
from bs4 import BeautifulSoup
from requests_html import AsyncHTMLSession
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix="$", intents=intents, case_insensitive=True)
async def amazon():
URL = "https://www.amazon.com/s?k=gaming"
with AsyncHTMLSession() as session:
response = await session.get(URL)
response.html.arender(timeout=20)
soup = BeautifulSoup(response.html.html, "lxml")
results = soup.select("a.a-size-base.a-link-normal.s-link-style.a-text-normal")
max_price = 10
for result in results:
price = result.text.split('$')[1].replace(",", "")
if float(price) < max_price:
print(f"Price: ${price}\nLink: https://www.amazon.com{result['href'].split('?')[0]}")
@client.command()
async def amaz(ctx):
await amazon()
await ctx.send("hello")
client.run("iputmytokenhere")
这是我在执行 $amaz 时遇到的错误:
RuntimeWarning: coroutine 'HTML.arender' was never awaited
response.html.arender(timeout=20)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
C:\Users\CK\AppData\Roaming\Python\Python37\site-packages\requests\sessions.py:428: RuntimeWarning: coroutine 'AsyncHTMLSession.close' was never awaited
self.close()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
我将其用作一个有趣的项目,非常感谢任何帮助。我尝试了很多东西,但似乎没有任何效果。我希望机器人将抓取的数据发送到 discord webhook。
+您可以使用以下代码实现amazon()
功能。
async def amazon():
URL = "https://www.amazon.com/s?k=gaming"
# add the string of the webhook url
WEBHOOK_URL = "https://your_webhook_url"
# change the with statement to assignment following the documentation
session = AsyncHTMLSession()
response = await session.get(URL)
# add await to prevent the "was not awaited" error
await response.html.arender(timeout=20)
# create the webhook object
webhook = Webhook.from_url(WEBHOOK_URL, adapter=AsyncWebhookAdapter(session))
soup = BeautifulSoup(response.html.html, "lxml")
results = soup.select("a.a-size-base.a-link-normal.s-link-style.a-text-normal")
max_price = 10
for result in results:
price = result.text.split('$')[1].replace(",", "")
if float(price) < max_price:
# change print() to webhook.send() to send the data from a webhook
await webhook.send(f"Price: ${price}\nLink: https://www.amazon.com{result['href'].split('?')[0]}")