我正在制作一個機器人,它接受用戶輸入(這是一個股票代碼),然后提供有關該特定股票的資訊。問題是,我不確定如何client.wait_input()在我的情況下使用 。這是我的機器人代碼:
# bot.py
import os
import SandPwebscraper as stocks
from dotenv import load_dotenv
from discord.ext import commands
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
bot = commands.Bot(command_prefix='$')
@bot.event
async def on_ready():
print(f'{bot.user.name} has connected to Discord!')
@bot.command(name = 'symbol', help = 'Type in all-caps symbol of company (ex. when searching for Apple, type AAPL).')
async def Symbol(ctx, companySymbol: str):
#
#
#
#
#
#
await ctx.send(''.join)
bot.run(TOKEN)
而且我試圖以某種方式包含以下stocks_input()功能SandPwebscraper.py:
# Web-scraped S&P 500 data for 500 US stocks.
import requests
import pandas as pd
from bs4 import BeautifulSoup
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62'}
url = 'https://www.slickcharts.com/sp500' # Data from SlickCharts
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.text, 'html.parser')
table1 = soup.find('table', attrs={'class':'table table-hover table-borderless table-sm'})
for row in table1.find_all('tr'):
all_td_tags = row.find_all('td')
if len(all_td_tags) > 0:
company = all_td_tags[1].text
symbol = all_td_tags[2].text
weight = all_td_tags[3].text
price = all_td_tags[4].text
chg = all_td_tags[5].text
perChg = all_td_tags[6].text
# print(company, '|', symbol, '|', weight, '|', price, '|', chg, '|', perChg)
df = pd.read_html(str(table1))[0] # Makes data into an html readable and can output and look through data
df.drop(['#'], axis = 1, inplace = True) # Removes an extra column of numbers
# pd.set_option('display.max_rows', None) # Removes cap on number of rows in viewall
def viewall():
allStocks = df
return allStocks
def appletest():
apple = df[df['Symbol'] == 'AAPL']
return apple
def stocks_input():
userinput = input('Enter company name or symbol: ')
if userinput.isupper():
symbol = (df[df['Symbol'] == userinput])
return symbol
else:
if userinput == 'viewall':
allStocks = df
return allStocks
else:
company = (df[df['Company'] == userinput])
return company
在這種情況下,如果userinput類似于“AAPL”,則將回傳以下內容:
Company Symbol Weight Price Chg % Chg
0 Apple Inc. AAPL 6.888732 177.65 -1.73 (-0.96%)
我應該在評論部分包含bot.py什么?
uj5u.com熱心網友回復:
該companySymbol變數已經存盤了用戶的輸入,因此您可以修改stocks_input()函式以獲取輸入。
def stocks_input(userinput):
if userinput.isupper():
symbol = (df[df['Symbol'] == userinput])
return symbol
else:
if userinput == 'viewall':
allStocks = df
return allStocks
else:
company = (df[df['Company'] == userinput])
return company
現在您可以使用companySymbol作為輸入來運行此函式:
@bot.command(name = 'symbol', help = 'Type in all-caps symbol of company (ex. when searching for Apple, type AAPL).')
async def Symbol(ctx, companySymbol: str):
values = stocks.stocks_input(companySymbol) # stocks_input function will return values and store it in 'values' variable
await ctx.send(values) # send values
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/400817.html
