From 708ab3a9f0401461590b76858331dca36349f41c Mon Sep 17 00:00:00 2001 From: Dronminator Date: Sun, 26 Mar 2023 00:42:17 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=97(=D0=BB=D0=B5=D0=B3=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Задания/task1/Lahin/postgresEasy.py | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Задания/task1/Lahin/postgresEasy.py diff --git a/Задания/task1/Lahin/postgresEasy.py b/Задания/task1/Lahin/postgresEasy.py new file mode 100644 index 0000000..375b40d --- /dev/null +++ b/Задания/task1/Lahin/postgresEasy.py @@ -0,0 +1,42 @@ +import psycopg2 +import wget +from bs4 import BeautifulSoup +from selenium import webdriver +from selenium.webdriver.chrome.service import Service +s = Service("С:\DATA\ChromeDriver\chromedriver.exe") +browser = webdriver.Chrome(service=s) +browser.get("https://online.metro-cc.ru/virtual/assortiment_rioba-4887?from=under_search&page=2") +html_text = browser.page_source +soup = BeautifulSoup(html_text, 'lxml') +productst = soup.find_all('span', class_="product-card-name__text") +pricest = soup.find_all('span', class_="product-price__sum-rubles") +pictures = soup.find_all('img', class_="product-card-photo__image") +connection=psycopg2.connect(host='localhost', dbname='dbdata', user='postgres', password='Q1w2e3r4') +cursor = connection.cursor() +products, prices = [], [] +for i in range(len(productst)): + products.append(productst[i].text.replace("\n", "").strip()) + prices.append(pricest[i].text+" Р/шт") +insert = """CREATE TABLE public.Chocolate( +id serial primary key, +product varchar(100), +price varchar(25), +src varchar(50) +); +""" +cursor.execute(insert) +connection.commit() +for i in range(len(products)): + url = pictures[i]['src'] + filename = f"C:\DATA\Images2\{i+1}.jpg" + wget.download(url, filename) + insert = f"""INSERT INTO public.Chocolate( + product, price, src) + VALUES + ('{products[i]}', '{prices[i]}', '{filename}');""" + cursor.execute(insert) + connection.commit() +cursor.execute("select * from Chocolate") +print("Результат", cursor.fetchall()) +cursor.close() +connection.close()