[英]Selenium data into discord using discord.py
我制作了一個跟蹤目標價格的機器人。com,我希望價格從 go 到 discord 這是原始代碼
import time
from selenium.common.exceptions import *
OOS = '//*[@id="notifyMe"]'
in_stock = '//*[@id="viewport"]/div[5]/div/div[2]/div[3]/div[1]/div/div[3]/div[1]/div[2]/button'
while True:
web = webdriver.Chrome()
web.get('https://www.target.com/p/minecraft-bee-pillow-buddy/-/A-79337175')
time.sleep(1)
try:
instock_button = web.find_element_by_xpath(in_stock).click()
print("INSTOCK")
except NoSuchElementException:
print("OOS")
web.quit()
這是我試圖在 discord 中鏈接的內容
import time
from discord.ext import *
from discord.ext import commands
from discord.ext import tasks
from discord.ext.commands import Bot
from discord_webhooks import DiscordWebhooks
from selenium import webdriver
from selenium.common.exceptions import *
in_stock = '//*[@id="viewport"]/div[5]/div/div[2]/div[3]/div[1]/div/div[3]/div[1]/div[2]/button'
bot = commands.Bot(command_prefix='!')
while True:
@bot.command()
async def weird(ctx):
channel = client.get_channel(784163508299890738)
web = webdriver.Chrome()
web.get('https://www.target.com/p/madden-nfl-20-playstation-4/-/A-54600234')
time.sleep(1)
try:
price = web.find_elements_by_xpath('//*[@id="viewport"]/div[5]/div/div[2]/div[2]/div[1]/div[1]/div[1]')
for post in price:
cost = (post.text)
instock_button = web.find_element_by_xpath(in_stock).click()
james = ("INSTOCK" + " PRICE:" + cost)
except NoSuchElementException:
marcus = ("OOS" + " PRICE:" + cost)
web.quit()
await channel.send(james)
client.run("TOKEN")
然而,當我運行這段代碼時,我得到了一個錯誤
raise CommandRegistrationError(command.name)
discord.ext.commands.errors.CommandRegistrationError:
The command weird is already an existing command or alias.
我該如何解決這個問題,請幫助!!!!!!!
您不能使用while True
因為您一次又一次地創建相同的命令。 除了discord
必須使用bot.run()
啟動自己的循環,同時運行兩個循環會產生問題。
幸運的是 discord 有task.loop(seconds=...)
定期運行一些代碼,所以它可以代替while True
工作。 它只需要啟動它。
我嘗試使用on_ready
啟動它,但它在我的 Linux 上不起作用,所以我不得不使用命令手動啟動它 - 但后來它一直運行
為了讓它更快,我只啟動Chrome
一次,並在停止機器人后關閉它。
import os
import time
from discord.ext import commands
from discord.ext import tasks
from discord.ext.commands import Bot
from selenium import webdriver
from selenium.common.exceptions import *
in_stock_xpath = '//*[@id="viewport"]/div[5]/div/div[2]/div[3]/div[1]/div/div[3]/div[1]/div[2]/button'
price_xpath = '//*[@id="viewport"]/div[5]/div/div[2]/div[2]/div[1]/div[1]/div[1]'
bot = commands.Bot(command_prefix='!')
#help(tasks.loop)
@tasks.loop(seconds=30)
async def weird():
print('[weird] start weird')
channel = bot.get_channel(784163508299890738) # `bot`, not `client`
web.get('https://www.target.com/p/madden-nfl-20-playstation-4/-/A-54600234')
time.sleep(5)
try:
price = web.find_elements_by_xpath(price_xpath)
for post in price:
cost = post.text
instock_button = web.find_element_by_xpath(in_stock_xpath).click()
message = "INSTOCK PRICE: " + cost
except NoSuchElementException:
cost = '???'
message = "OOS PRICE: " + cost
await channel.send(message)
@bot.event
async def on_ready(): # this doesn't start on my computer
print('[on_ready] start weird')
weird.start()
@bot.command()
async def start(ctx): # I start it manually with command `!start`
print('[start] start weird')
weird.start()
# --- main ---
print('start chrome')
web = webdriver.Chrome() # create only once
print('start bot')
TOKEN = os.getenv('DISCORD_TOKEN')
bot.run(TOKEN) # `bot`, not `client`
print('stop bot')
web.quit() # close only once
print('stop chrome')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.