From 0e471eed814b6b15b26483b5cfd897e358bc3ef0 Mon Sep 17 00:00:00 2001 From: polevev <124861891+polevev@users.noreply.github.com> Date: Tue, 28 Mar 2023 21:35:22 +0300 Subject: [PATCH] Create Parsing --- Задания/task1/Markovich/Parsing | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Задания/task1/Markovich/Parsing diff --git a/Задания/task1/Markovich/Parsing b/Задания/task1/Markovich/Parsing new file mode 100644 index 0000000..62aaee7 --- /dev/null +++ b/Задания/task1/Markovich/Parsing @@ -0,0 +1,40 @@ +from bs4 import BeautifulSoup +from selenium import webdriver +from selenium.webdriver.chrome.service import Service +import wget +import psycopg2 + +connection = psycopg2.connect(host='localhost', dbname='dbdata', user='postgres', password='Q1w2e3r4') +cursor = connection.cursor() + +s = Service(r"C:\\Documents\\data\\chromedriver.exe") +browser = webdriver.Chrome(service=s) +browser.get('https://www.citilink.ru/catalog/smartfony/') +html_text = browser.page_source +b_soup = BeautifulSoup(html_text, 'lxml') + +insert_qwery = """create table phones\n +(id serial primary key, name varchar(100), price varchar(100),delivery varchar(100), pictures varchar(100))""" + +cursor.execute(insert_qwery) +connection.commit() + +phone_names = b_soup.find_all('a', class_="app-catalog-9gnskf e1259i3g0") +phone_price = b_soup.find_all('span', class_="e1j9birj0 e106ikdt0 app-catalog-j8h82j e1gjr6xo0") +phone_delivery = b_soup.find_all('div', class_="e16h9msl0 app-catalog-1fnzrtt e15ufesp0") +pictures = b_soup.find_all('div', class_="app-catalog-zugobn e1o1y6qd0") + +for i in range(15): + url = 'https://www.citilink.ru/catalog/smartfony/' + filename = f"C:\\Users\\mark6\\OneDrive\\Desktop\\БД\\img\\{i}.jpg" + wget.download(url, filename) + ins_qwery = f"""INSERT INTO public.phones(name, price, delivery, pictures)\n + VALUES ('{phone_names[i].text}', '{phone_price[i].text}', '{phone_delivery[i].text}', '{filename}');""" + cursor.execute(ins_qwery) + connection.commit() + +cursor.execute("Select * from phones") +print(cursor.fetchall()) + +cursor.close() +connection.close()