【发布时间】:2021-08-18 10:50:09
【问题描述】:
我用python写了一个机器人,机器人功能:显示股票价格,设置价格限制,当达到这个限制时,向discord发送消息。在这个阶段,机器人只能监控一个动作。如何使其异步,以便它可以同时监控多只股票,并且当达到限制时,它会发送特定股票与价格已达到标记的消息不一致。
from bs4 import BeautifulSoup
import discord
from discord.ext import commands
from config import settings
bot = commands.Bot(command_prefix = settings['prefix'], help_command=None)
@bot.event
async def on_message(message):
await bot.process_commands(message)
channel = message.channel
co = '{0.content}'.format(message).split()
tes=co[0]
if tes=='price':
yo=co[1]
print(yo)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0'
}
r = requests.get(
('https://finance.yahoo.com/quote/') + yo + ('?p=') + yo + ('.tsrc=fin-srch'),
headers=headers)
soup = BeautifulSoup(r.text, 'lxml')
content = soup.find('div', {"class": 'My(6px) Pos(r) smartphone_Mt(6px)'}).find('span').text
print(content)
await channel.send(f'{yo} - {content}')
return content
elif tes=='limit':
p=co[1]
su=co[2]
price=float(su)
while True:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0'
}
r = requests.get(
('https://finance.yahoo.com/quote/') + p + ('?p=') + p + ('.tsrc=fin-srch'),
headers=headers)
soup = BeautifulSoup(r.text, 'lxml')
content = soup.find('div', {"class": 'My(6px) Pos(r) smartphone_Mt(6px)'}).find('span').text
con=float(content)
if price<=con:
await channel.send("достиг")
break
print(content)
return content
bot.run(settings['token'])
【问题讨论】:
标签: python multithreading discord discord.py python-asyncio