import telebot
import requests
from bs4 import BeautifulSoup
from telebot import types
bot = telebot.TeleBot('???')
def get_product_data(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
title = soup.find('h1', class_='same-part-kt__header').text.strip()
price = soup.find('span', class_='price-block__final-price').text.strip()
old_price_element = soup.find('del', class_='price-block__old-price')
old_price = old_price_element.text.strip() if old_price_element else None
discount_element = soup.find('span', class_='price-block__discount')
discount = discount_element.text.strip() if discount_element else None
rating = soup.find('span', class_='user-opinion__rating').text.strip()
reviews = soup.find('span', class_='same-part-kt__count-review').text.strip()
images = [img['src'] for img in soup.find_all('img', class_='photo-zoom__preview')][:5]
description = soup.find('p', class_='collapsable__text').text.strip()
return {
'title': title,
'price': price,
'old_price': old_price,
'discount': discount,
'rating': rating,
'reviews': reviews,
'images': images,
'description': description
}
@bot.message_handler(commands=['product'])
def send_product_card(message):
try:
url = message.text.split()[1]
product_data = get_product_data(url)
message_text = f"*{product_data['title']}*\n" \
f"Цена: {product_data['price']}\n"
if product_data['old_price']:
message_text += f"Старая цена: {product_data['old_price']}\n"
if product_data['discount']:
message_text += f"Скидка: {product_data['discount']}\n"
message_text += f"Рейтинг: {product_data['rating']}\n" \
f"Отзывы: {product_data['reviews']}\n" \
f"Описание: {product_data['description']}\n" \
f"[Ссылка на товар]({url})\n"
for img in product_data['images']:
bot.send_photo(chat_id=message.chat.id, photo=f"https:{img}")
bot.send_message(chat_id=message.chat.id, text=message_text, parse_mode='Markdown')
except Exception as e:
bot.send_message(chat_id=message.chat.id, text=f"Ошибка: {e}")
bot.polling()
PS код бота заменил на ??? что бы приколисты ничего не поломали. Вот сам код:
import telebot
botTimeWeb = telebot.TeleBot('???')
bot = telebot.TeleBot("???")
from telebot import types
def get_product_data(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
title = soup.find('h1').text.strip()
price = soup.find('span', class_='price').text.strip()
old_price = soup.find('span', class_='old-price').text.strip()
discount = soup.find('span', class_='discount').text.strip()
rating = soup.find('span', class_='rating').text.strip()
reviews = soup.find('span', class_='reviews').text.strip()
images = [img['src'] for img in soup.find_all('img', class_='product-image')][:5]
description = soup.find('div', class_='description').text.strip()
return {
'title': title,
'price': price,
'old_price': old_price,
'discount': discount,
'rating': rating,
'reviews': reviews,
'images': images,
'description': description
}
@bot.message_handler(commands=['product'])
def send_product_card(message):
url = message.text.split()[1]
product_data = get_product_data(url)
message_text = f"*{product_data['title']}*\n" \
f"Цена: {product_data['price']} (была: {product_data['old_price']})\n" \
f"Скидка: {product_data['discount']}\n" \
f"Рейтинг: {product_data['rating']}\n" \
f"Отзывы: {product_data['reviews']}\n" \
f"Описание: {product_data['description']}\n" \
f"[Ссылка на товар]({url})\n"
for img in product_data['images']:
bot.send_photo(chat_id=message.chat.id, photo=img)
bot.send_message(chat_id=message.chat.id, text=message_text, parse_mode='Markdown')
bot.polling()
Код запускается но бот в тг не реагирует ни на что, прошу помочь и желательно объяснить, что бы в преть я больше не искал ответ