From 21ad5c1cbe929bf604a9771b87358afaf9dd8e24 Mon Sep 17 00:00:00 2001 From: Axeten <124861675+Axeten@users.noreply.github.com> Date: Wed, 29 Mar 2023 21:41:31 +0300 Subject: [PATCH] Create bd.py --- Задания/task1/romanov/bd.py | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Задания/task1/romanov/bd.py diff --git a/Задания/task1/romanov/bd.py b/Задания/task1/romanov/bd.py new file mode 100644 index 0000000..ac66be0 --- /dev/null +++ b/Задания/task1/romanov/bd.py @@ -0,0 +1,44 @@ +import psycopg2 +import wget +from bs4 import BeautifulSoup +from selenium import webdriver +from selenium.webdriver.chrome.service import Service +connection = psycopg2.connect(host='localhost', dbname='postgres', + user='postgres', password='Q1w2e3r4') + +cursor = connection.cursor() + +cr_qwery = """ create table avito + (id serial primary key, + name varchar(60), + price varchar(15), + picture varchar(100))""" + +cursor.execute(cr_qwery) +connection.commit() + +driver = Service('C:\Desktop\exe\chromedriver.exe') +browser = webdriver.Chrome(service=driver) +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('div', class_='product-card__title') +price = soup.find_all('div', class_='product-card__price') +picture = soup.find_all('div', class_='product-slider__slide-img swiper-lazy swiper-lazy-loaded') + +for i in range(len(name)): + url = picture[i].find('img').attrs['src'] + filename = f"C:\\Users\\Михаил\PycharmProjects\pythonProject8\\venv\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()