在 python 中使用电报机器人进行多处理
Multiprocessing with telegram bot in python
我正在为我的客户做一个非常大的项目:
我的脚本必须检查用户在电报机器人聊天中指定的 link 产品在亚马逊上的可用性。
当脚本获得 link 以检查可用性时,它会每 10 分钟使用 selenium 和 python 访问该 link,甚至在 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 多处理模块
我正在为我的客户做一个非常大的项目: 我的脚本必须检查用户在电报机器人聊天中指定的 link 产品在亚马逊上的可用性。 当脚本获得 link 以检查可用性时,它会每 10 分钟使用 selenium 和 python 访问该 link,甚至在 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 多处理模块