我正在为我的客户做一个非常大的项目:我的脚本必须检查亚马逊上产品的可用性,用户在电报机器人的聊天中指定了该产品的链接。当脚本获得一个链接以检查可用性时,它会使用selenium和python每10分钟访问一次该链接,并且当产品在2个月后可用时。机器人程序通知产品可用。问题是,当脚本搜索可用性时,它不会读取通过电报发送给它的其他消息,直到产品可用并通知用户。有没有办法:总是阅读聊天内容,每当有人要求进行耗时的过程时,都要将该操作作为一个单独的过程来启动
这是我的代码:
import time
import telebot
import requests
import bs4
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException
import time
import random
chrome_options = Options()
chrome_options.add_argument('start-maximized')
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(options=chrome_options)
driver.implicitly_wait(2)
TOKEN = "MY_TOKEN"
bot = telebot.TeleBot(token=TOKEN)
help = '''
AddCart - Add product (link/links) to cart
Details - Give details of product (link/links)
'''
@bot.message_handler(commands=['help']) # welcome message handler
def send_welcome(message):
bot.reply_to(message, help)
@bot.message_handler(func=lambda msg: msg.text is not None and 'AddCart' in msg.text)
def add_cart(message):
mes = message.text.replace("AddCart","").replace("n","").replace(" ","")
links = mes.split('^^')
for i in links:
driver.get(i)
try:
addcart = driver.find_element_by_xpath('//*[@id="add-to-cart-button"]')
addcart.click()
print("Done")
bot.reply_to(message, "Added to cart")
except:
productname = ""
print("Coulndt do")
bot.reply_to(message, "Could not add to cart..")
@bot.message_handler(func=lambda msg: msg.text is not None and 'Details' in msg.text)
def scrape_info(message):
mes = message.text.replace("Details","").replace("n","").replace(" ","")
links = mes.split(' ')
print("DONE")
for i in links:
driver.get(i)
time.sleep(0.2)
try:
productname = driver.find_element_by_xpath('//*[@id="productTitle"]').text
except Exception as e:
print(e)
productname = ""
try:
productprice = driver.find_element_by_xpath('//*[@id="priceblock_ourprice"]').text
except:
try:
productprice = driver.find_element_by_xpath('//*[@id="priceblock_saleprice"]').text
except:
productprice = ""
try:
stock = driver.find_element_by_xpath('//*[@id="availability"]/span').text.lower()
except:
stock = ""
try:
imageurl = driver.find_element_by_xpath('//*[@id="landingImage"]').get_attribute('src')
except:
imageurl = ""
print("Done")
bot.reply_to(message, f"Name: {productname}nPrice: {productprice}nAvailibility: {stock}n{imageurl}")
@bot.message_handler(func=lambda msg: msg.text is not None and 'CheckAvail' in msg.text)
def check_availibility(message):
mes = message.text.replace("CheckAvail","").replace("n","").replace(" ","")
link = mes
for i in links:
driver.get(i)
time.sleep(600)
try:
stock = driver.find_element_by_xpath('//*[@id="availability"]/span').text.lower()
if stock=="in stock":
bot.reply_to(message, "Product is Availible")
except Exception as e:
print(e)
stock = ""
while True:
try:
bot.polling(none_stop=True)
except Exception as e:
print(f"[EXCEPTION] {e}")
time.sleep(15)
你能帮我做这个吗。如果有解决方案,请根据解决方案编辑我的脚本。提前感谢
您必须使用python多处理模块