From 265ffac7f037b7fb435aadf7522d75afe88005ce Mon Sep 17 00:00:00 2001 From: Harutyun Date: Tue, 28 Mar 2023 22:31:53 +0300 Subject: [PATCH] parser with database --- Задания/task1/Garanyan/main.py | 39 +++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/Задания/task1/Garanyan/main.py b/Задания/task1/Garanyan/main.py index ac6f8f2..8e6da15 100644 --- a/Задания/task1/Garanyan/main.py +++ b/Задания/task1/Garanyan/main.py @@ -1,12 +1,35 @@ from bs4 import BeautifulSoup from selenium import webdriver from selenium.webdriver.chrome.service import Service +import psycopg2 +import wget -s = Service("E:\data\chromedriver.exe") -browser = webdriver.Chrome(service=s) -browser.get("https://ria.ru/") -html_text = browser.page_source -soup = BeautifulSoup(html_text, 'lxml') -news = soup.find_all('div', class_='cell cell-list') -for new in news: - print(new.text) +browser = webdriver.Chrome(service=Service('E:\data\chromedriver.exe')) +browser.get('https://8956food.ru/') +soup = BeautifulSoup(browser.page_source, "lxml") + +Name = soup.find_all(attrs={"class": "subtitle"}) +Price = soup.find_all(attrs={"class": "price"}) +Image = soup.find_all(attrs={"class": "holder-img"}) + +connection = psycopg2.connect(host='localhost', dbname='dbdata', user='postgres', password='Q1w2e3r4') +cursor = connection.cursor() +create_q = '''CREATE TABLE Food + (ID serial primary key, Name varchar(500), Price varchar(60), src varchar(100))''' +cursor.execute(create_q) +connection.commit() + +#https://smartomato.ams3.cdn.digitaloceanspaces.com/uploads/media/photo/769221/dish_large__1.jpg +for j in range(15): + url = Image[j].find('img').attrs['src'] + tempf = f"C:\\Users\\Harutyun\\Desktop\\prog\\food{j}.jpg" + wget.download(url, tempf) + insert_query = f'''INSERT into public.Food(Name, Price, src) values ('{Name[j].text}', '{Price[j].text}', '{tempf}') ''' + cursor.execute(insert_query) +connection.commit() + +cursor.execute("SELECT * from Food") +print(cursor.fetchall()) + +cursor.close() +connection.close() \ No newline at end of file