Пытаюсь сделать бота на python, который будет принимать ссылки на сайты, а отправлять в ответ скриншот этого сайта
Основная ошибка в том, что мой бот то не отвечает вообще на ссылку, то выдает ошибки по типу: WebDriver.init() got an unexpected keyword argument 'executable_path' или Message: unknown error: cannot find Chrome binary
Вот код:
from telegram import Update
from telegram.ext import Updater, Command_Handler, Message_Handler, Filters, Callback_Context
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
import os
from PIL import Image
# Функция для создания скриншота страницы
def capture_screenshot(url):
options = Options()
options.binary_location = "C:\\Program Files\\Chrome\\chrome64_55.0.2883.75\\chrome.exe"
driver = webdriver.Chrome(options=options, executable_path=r'C:\path\to\chromedriver.exe')
driver.get('http://google.com/')
driver.quit()
return screenshot_path
# Настройка драйвера
with webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options) as driver:
driver.get(url)
screenshot_path = "screenshot.png"
driver.save_screenshot(screenshot_path)
return screenshot_path
# Обработчик сообщений с ссылкой
def handle_message(update: Update, context: CallbackContext) -> None:
url = update.message.text
if url.startswith("http://") or url.startswith("https://"):
try:
# Создаем скриншот
screenshot_path = capture_screenshot(url)
# Отправляем скриншот в ответ
with open(screenshot_path, 'rb') as photo:
update.message.reply_photo(photo)
# Удаляем скриншот после отправки
os.remove(screenshot_path)
except Exception as e:
update.message.reply_text(f"Не удалось создать скриншот. Ошибка: {e}")
else:
update.message.reply_text("Пожалуйста, отправьте корректную ссылку на веб-сайт.")
# Стартовая команда
def start(update: Update, context: CallbackContext) -> None:
update.message.reply_text("Привет! Отправь мне ссылку, и я сделаю скриншот страницы для тебя.")
# Основная функция
def main():
TOKEN = "7858227532:AAF0f1uKLJQS0RVzvBGeKnfKFa9PBq3-JSw"
updater = Updater(TOKEN)
# Регистрация обработчиков
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start))
dp.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_message))
# Запуск бота
updater.start_polling()
updater.idle()
if __name__ == "__main__":
main()