【问题标题】:Selenium data into discord using discord.py使用 discord.py 将 Selenium 数据转换为不和谐
【发布时间】:2021-04-25 05:19:01
【问题描述】:

我制作了一个跟踪 target.com 价格的机器人,我希望价格通过不和谐这里是原始代码

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()

这是我试图让它在不和谐中链接的内容

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.

我该如何解决这个问题请大家帮忙!!!!!!

【问题讨论】:

  • 始终将完整的错误消息(从单词“Traceback”开始)作为文本(不是屏幕截图)提出问题(不是评论)。还有其他有用的信息。
  • 你为什么在 while True 中运行 command ?您一次又一次地创建相同的command。它不会多次运行command。这不是个好主意。您宁愿仅在单独的头中使用selenium 运行while True - 并将prices 保存在全局变量中。而command 应该只创建一次,它应该从全局变量中获取prices
  • 如果你只创建一次webdriver 会更快 - 在while True 之前 - 你不关闭它而是一次又一次地使用它 - 并在程序结束时关闭它(aftet离开while True)
  • Sending message every minute in discord.py - 使用task.loop() 代替while Truesleep

标签: python python-3.x selenium web-scraping discord


【解决方案1】:

您不能使用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')

【讨论】:

    猜你喜欢
    • 2022-01-14
    • 2020-05-25
    • 2023-03-21
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    相关资源
    最近更新 更多