【发布时间】:2021-01-10 00:36:57
【问题描述】:
如果满足某些条件,我正试图让我的机器人向不和谐频道发送消息,但我似乎无法让代码正常工作。代码每 5 秒检查一次列表是否包含字符串“.12”。然后应该转发消息。
import requests
import time
import discord
from discord.ext import commands, tasks
from bs4 import BeautifulSoup
while True:
client = commands.Bot(command_prefix='.')
@client.event
async def on_ready():
print('bot is active')
url = 'website link'
res = requests.get(url)
html = res.text
soup = BeautifulSoup(html, 'html.parser')
html_element = soup.find_all( 'td', { "class" : "eksam-ajad-aeg" } )
ret = []
for t in html_element:
ret.append(t.text)
print(ret)
if '.12.' in ret:
@client.event
async def send():
channel = client.get_channel(758088198852182037)
await channel.send('message')
client.run('token')
time.sleep(5)
【问题讨论】:
-
将你的机器人放入
while True似乎有点不寻常。一般情况下,大多数人最后只打一次client.run()。我不确定这是否是您的问题的原因,但我注意到您的机器人有点不寻常。编辑:我想我知道你现在想要做什么。 Asyncio 可能是解决此问题的更好方法:docs.python.org/3/library/asyncio.html 特别是asyncio.sleep部分。
标签: python web-scraping discord