【发布时间】: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 True和sleep
标签: python python-3.x selenium web-scraping discord