From 775a3f58a5313219fe01698bbdfe7ef34b233c09 Mon Sep 17 00:00:00 2001 From: Dronminator Date: Sat, 15 Apr 2023 13:18:11 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A2=D0=B5=D0=BB=D0=B5=D0=B3=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=20=D0=91=D0=BE=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Задания/task1/Lahin/postgres.py | 19 ++++++++------- Задания/task1/Lahin/telegrambot.py | 37 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 Задания/task1/Lahin/telegrambot.py diff --git a/Задания/task1/Lahin/postgres.py b/Задания/task1/Lahin/postgres.py index cfa0920..3afaf45 100644 --- a/Задания/task1/Lahin/postgres.py +++ b/Задания/task1/Lahin/postgres.py @@ -4,6 +4,7 @@ from bs4 import BeautifulSoup from selenium import webdriver from selenium.webdriver.chrome.service import Service import time + i = 1 s = Service("С:\DATA\ChromeDriver\chromedriver.exe") browser = webdriver.Chrome(service=s) @@ -24,12 +25,12 @@ features = soup.find_all('span', class_="product-feature-list__value") pictures = soup.find_all('div', class_="product-picture-container") connection=psycopg2.connect(host='localhost', dbname='dbdata', user='postgres', password='Q1w2e3r4') cursor = connection.cursor() -""" insert = CREATE TABLE public.laptops( +insert = """CREATE TABLE public.laptops( id serial primary key, -product varchar(100), -price varchar(15), -diagonal varchar(5), -resolution varchar(20), +Product varchar(100), +Price varchar(15), +Diagonal varchar(5), +Resolution varchar(20), CPU varchar(50), RAM varchar(15), Graphics_Controller varchar(40), @@ -37,7 +38,7 @@ Volume varchar(25), src varchar(100) ); """ -insert = """TRUNCATE TABLE public.laptops; ALTER SEQUENCE laptops_id_seq RESTART WITH 1;""" + cursor.execute(insert) connection.commit() for i in range(len(productst)): @@ -49,13 +50,11 @@ for i in range(len(productst)): wget.download(url, filename) t = pricest[i].text.replace("\xa0", " ") insert = f"""INSERT INTO public.laptops( - product, price, diagonal, resolution, CPU, RAM, Graphics_Controller, Volume, src) + Product, Price, Diagonal, Resolution, CPU, RAM, Graphics_Controller, Volume, src) VALUES ('{productst[i].text.strip()}', '{t.strip()}', '{u[0][:u[0].find("/")]}', '{u[0][u[0].find("/")+1:]}', '{u[2]}', '{u[4]+" "+u[5]}', '{u[6]}', '{u[8]}', '{filename}');""" cursor.execute(insert) connection.commit() -cursor.execute("select * from laptops") -print("Результат", cursor.fetchall()) cursor.close() -connection.close() +connection.close() \ No newline at end of file diff --git a/Задания/task1/Lahin/telegrambot.py b/Задания/task1/Lahin/telegrambot.py new file mode 100644 index 0000000..b09b60e --- /dev/null +++ b/Задания/task1/Lahin/telegrambot.py @@ -0,0 +1,37 @@ +import telebot +import psycopg2 +def execute_request(request): + connection = psycopg2.connect(host='localhost', dbname='dbdata', user='postgres', password='Q1w2e3r4') + cursor = connection.cursor() + cursor.execute(request) + result = cursor.fetchall() + cursor.close() + connection.close() + return result +max_id = int(execute_request("select count(*) from laptops")[0][0]) +bot = telebot.TeleBot('5656574233:AAEU1Ggc4J1aWMtlLnphtScDd3jGCrzbuuY') +@bot.message_handler(commands=["start"]) +def start(m, res=False): + bot.send_message(m.chat.id, f'Привет. Введите идентификатор от 1 до {max_id}, чтобы получить данные: ') + +@bot.message_handler(content_types=["text"]) +def from_bd(message): + try: + current_id = int(message.text) + except BaseException: + bot.send_message(message.chat.id, "Вы неправильно ввели ID. Попробуйте снова:") + return + if current_id > max_id: + bot.send_message(message.chat.id, "ID больше, чем количество товаров. Попробуйте снова:") + return + else: + data = execute_request(f"select * from laptops where id = {current_id}")[0][1:-1] + names_of_columns = [i[0] for i in execute_request(f"""select Column_name from Information_schema.columns where Table_name = 'laptops'""")][1:-1] + everydata = [] + for data_name, column_name in zip(data, names_of_columns): + everydata.append(column_name.capitalize() + ": " + data_name) + bot.send_message(message.chat.id, "\n\n".join(everydata)) + bot.send_message(message.chat.id, f"Введите следующий ID от 1 до {max_id}:") +bot.polling(none_stop=True, interval=0) + +