This commit is contained in:
Axeten
2023-05-22 23:31:17 +03:00
parent 68af047cb4
commit 88167c3821
3 changed files with 102 additions and 0 deletions
+14
View File
@@ -0,0 +1,14 @@
try:
2/0
except ZeroDivisionError as a:
print('ofibka delit na nool', a)
except:
print("Ofibka")
else:
print('ofibok net')
finally:
print("выполнятеся в любом случае")
#def summ(a,b):
# c=a+b
# return c
@@ -0,0 +1,40 @@
import telebot
import psycopg2
import config
from telebot import types
import random
import parshell
connection = psycopg2.connect(host='localhost', dbname='dbdata', user='postgres', password='Q1w2e3r4')
cursor = connection.cursor()
sel_query = """SELECT * FROM public.parser"""
cursor.execute(sel_query)
array = list(cursor.fetchall())
bot = telebot.TeleBot('5803518584:AAEDCHZdvz-RurPpxJhVV0MNeCxWDVU5czk')
@bot.message_handler(commands=["start"])
def start(m, res=False):
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
item1 = types.KeyboardButton("Случайная доска")
item2 = types.KeyboardButton("П")
markup.add(item1)
markup.add(item2)
bot.send_message(m.chat.id,
'"Случайная доска" (название, цена, фото)\n "П" - д',
reply_markup=markup)
@bot.message_handler(content_types=["text"])
def handle_text(message):
if (message.text.strip() == 'Случайная доска'):
i = random.choice(array)
ans = f'Название: {i[1]}\nЦена: {i[2]}\n'
bot.send_message(message.chat.id, ans)
bot.send_photo(message.chat.id, open(i[3], 'rb'))
elif (message.text.strip() == 'П'):
bot.send_message(message.chat.id, 'Вы нажали кнопку п: ' + message.text)
bot.polling(none_stop=True, interval=0)
@@ -0,0 +1,48 @@
import wget
import psycopg2
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
connection = psycopg2.connect(host='localhost', dbname='dbdata',
user='postgres', password='Q1w2e3r4')
cursor = connection.cursor()
cr_qwery = """ create table parser
(id serial primary key,
name varchar(60),
price varchar(15),
picture varchar(100))"""
try:
cursor.execute(cr_qwery)
connection.commit()
except psycopg2.errors.DuplicateTable:
print("duplicate of the table")
s = Service('C:\Desktop\exe\chromedriver.exe')
browser = webdriver.Chrome(service=s)
browser.get('https://www.hellride.ru/catalog/zapchasti-dlya-tryukovyh-samokatov/deki')
html_code = browser.page_source
soup = BeautifulSoup(html_code, 'lxml')
name = soup.find_all('span', class_='product-card__title')
price = soup.find_all('span', class_='product-card__price')
picture = soup.find_all('img', class_='product-slider__slide-img swiper-lazy swiper-lazy-loaded')
for prices, names in zip(price, name,):
print(f" название {names.text} ; цена: {prices.text} ")
for i in range(len(name)):
url = picture[i].attrs['src']
filename = f"Pictures/{i}.jpg"
wget.download(url, filename)
ins_qwery = f"""insert into public.Parser(name, price, picture)
values ('{name[i].text}', '{price[i].text}', '{filename}')"""
cursor.execute(ins_qwery)
connection.commit()
cursor.close()
connection.close()