added function

This commit is contained in:
ada-dmitry
2023-04-22 23:51:27 +03:00
parent c9b158dce9
commit f59470def3
7 changed files with 55 additions and 36 deletions
+14
View File
@@ -0,0 +1,14 @@
def sel(cursor, tabl):
query = f"""SELECT * FROM public.{tabl}"""
cursor.execute(query)
array = list(cursor.fetchall())
return array
def upd(conn, cursor, tabl):
import parser
for i in range(15):
up_query = f"""UPDATE {tabl} SET page_name = '{parser.name[i].text}', price = '{parser.price[i].text}', priceDis = '{parser.priceDis[i].text}', mark = '{parser.mark[i].text}' WHERE id = {i+1}"""
cursor.execute(up_query)
conn.commit()
Binary file not shown.
-2
View File
@@ -14,5 +14,3 @@ price = b_soup.find_all('span', class_="middle_price")
priceDis = b_soup.find_all('span', class_="baseoldprice") priceDis = b_soup.find_all('span', class_="baseoldprice")
mark = b_soup.find_all('span', class_="product-rating__rating") mark = b_soup.find_all('span', class_="product-rating__rating")
pictures = b_soup.find_all('div', class_="catalog-list-item__img-wrapper") pictures = b_soup.find_all('div', class_="catalog-list-item__img-wrapper")
+5 -6
View File
@@ -4,12 +4,12 @@ import parser
import config import config
connection = psycopg2.connect(host=config.hostname, dbname=config.databname, user=config.username, password=config.passw) connection = psycopg2.connect(
host=config.hostname, dbname=config.databname, user=config.username, password=config.passw)
cursor = connection.cursor() cursor = connection.cursor()
creat_qwery = """ create table Parser creat_qwery = """ create table Parser
(id serial primary key, page_name varchar(100), price varchar(10), priceDis varchar(30), mark varchar(10), scr varchar(100))""" (id serial primary key, page_name varchar(100), price varchar(10), priceDis varchar(30), mark varchar(10), scr varchar(100))"""
# cursor.execute(creat_qwery)
# connection.commit()
try: try:
cursor.execute(creat_qwery) cursor.execute(creat_qwery)
connection.commit() connection.commit()
@@ -19,7 +19,8 @@ except psycopg2.errors.DuplicateTable:
for i in range(15): for i in range(15):
print(i) print(i)
url = 'https://amwine.ru' + parser.pictures[i].find('a').find('img').attrs['data-src'] url = 'https://amwine.ru' + \
parser.pictures[i].find('a').find('img').attrs['data-src']
filename = f"Python\\TeleBot\img\{i}.jpg" filename = f"Python\\TeleBot\img\{i}.jpg"
wget.download(url, filename) wget.download(url, filename)
try: try:
@@ -29,6 +30,4 @@ for i in range(15):
except: except:
connection.rollback() connection.rollback()
cursor.close()
connection.close()
+12 -7
View File
@@ -1,9 +1,14 @@
def f(): import psycopg2
a = [1,2,3] import wget
b = [3,4,5] import config
c = ["1", "2", "3"]
return a,b,c
a,b,c = f()
print(a, b, c) connection = psycopg2.connect(host=config.hostname, dbname=config.databname, user=config.username, password=config.passw)
cursor = connection.cursor()
def sel(a):
s_query = f"""SELECT * FROM public.parser WHERE id = {a}"""
print(cursor.execute(s_query))
return cursor.fetchone()
print(sel(10))
+24 -21
View File
@@ -3,42 +3,45 @@ import psycopg2
import config import config
from telebot import types from telebot import types
import random import random
import FuncForBot
connection = psycopg2.connect(host=config.hostname, dbname=config.databname, user=config.username, password=config.passw) connection = psycopg2.connect(
host=config.hostname, dbname=config.databname, user=config.username, password=config.passw)
cursor = connection.cursor() cursor = connection.cursor()
try: try:
sel_query = """SELECT * FROM public.parser""" array = FuncForBot.sel(cursor, 'parser')
cursor.execute(sel_query)
except psycopg2.errors.UndefinedTable: except psycopg2.errors.UndefinedTable:
connection.rollback() connection.rollback()
import tableCreator import tableCreator
sel_query = """SELECT * FROM public.parser""" array = FuncForBot.sel(cursor, 'parser')
cursor.execute(sel_query) finally:
FuncForBot.upd(connection, cursor, 'parser')
array = list(cursor.fetchall()) cursor.close()
connection.close()
bot = telebot.TeleBot(config.api) bot = telebot.TeleBot(config.api)
@bot.message_handler(commands=["start"])
@bot.message_handler(commands=["start"])
def start(m, res=False): def start(m, res=False):
markup=types.ReplyKeyboardMarkup(resize_keyboard=True) markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
item1=types.KeyboardButton("Рандомное вино!") item1 = types.KeyboardButton("Рандомное вино!")
item2=types.KeyboardButton("Повтори!") item2 = types.KeyboardButton("Повтори!")
markup.add(item1) markup.add(item1)
markup.add(item2) markup.add(item2)
bot.send_message(m.chat.id, '"Рандомное вино!" - для получения карточки товара\n "Повтори!" - для повторения сообщений.', reply_markup=markup) bot.send_message(
m.chat.id, '"Рандомное вино!" - для получения карточки товара\n "Повтори!" - для повторения сообщений.', reply_markup=markup)
@bot.message_handler(content_types=["text"]) @bot.message_handler(content_types=["text"])
def handle_text(message): def handle_text(message):
if(message.text.strip() == 'Рандомное вино!'): if (message.text.strip() == 'Рандомное вино!'):
i = random.choice(array) i = random.choice(array)
ans = f'Название: {i[1]}\nЦена: {i[2]}\nЦена без скидки: {i[3]}\nОценка: {i[4]}\n' ans = f'Название: {i[1]}\nЦена: {i[2]}\nЦена без скидки: {i[3]}\nОценка: {i[4]}\n'
bot.send_message(message.chat.id, ans) bot.send_message(message.chat.id, ans)
bot.send_photo(message.chat.id, open(i[5], 'rb')) bot.send_photo(message.chat.id, open(i[5], 'rb'))
elif(message.text.strip() == 'Повтори!'): elif (message.text.strip() == 'Повтори!'):
bot.send_message(message.chat.id, 'Ввод: '+ message.text) bot.send_message(message.chat.id, 'Ввод: ' + message.text)
bot.polling(none_stop=True, interval=0) bot.polling(none_stop=True, interval=0)