Домашнее задание 1 (Парсинг)

This commit is contained in:
nikitin712
2023-03-26 21:12:17 +03:00
parent 377dda950a
commit 4499d6d8ae
+32 -2
View File
@@ -1,12 +1,42 @@
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from selenium import webdriver from selenium import webdriver
from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.service import Service
import psycopg2
import wget
connection = psycopg2.connect(host='localhost', dbname='dbdata', user='postgres', password='Q1w2e3r4')
cursor = connection.cursor()
s = Service('D:\Games\data\chromedriver.exe') s = Service('D:\Games\data\chromedriver.exe')
browser = webdriver.Chrome(service=s) browser = webdriver.Chrome(service=s)
browser.get('https://www.volkswagen.ru/polo/') browser.get('https://www.volkswagen.ru/polo/')
html_text = browser.page_source html_text = browser.page_source
soup = BeautifulSoup(html_text, 'lxml') soup = BeautifulSoup(html_text, 'lxml')
creat_table = """ create table Cars_volks
(id serial primary key, car_name varchar(20),
price varchar(15), adress varchar(40),
scr varchar(100)) """
#cursor.execute(creat_table)
connection.commit()
car_names = soup.find_all('div', class_='avn001-2_name') car_names = soup.find_all('div', class_='avn001-2_name')
prices = soup.find_all('div', class_='avn001-2_price-container') prices = soup.find_all('div', class_='avn001-2_price-container')
for car_name, price in zip(car_names, prices): adresses = soup.find_all('div', class_='avn001-2_dealer-link__text')
print(f"Название машины:{car_name.text} | Цена: {price.text} рублей") pictures = soup.find_all('div', class_='avn001-2_image image__container')
#for car_name, adress, price in zip(car_names, adresses, prices):
# print(f"Название машины:{car_name.text} | Адрес диллера: {adress.text} | Цена: {price.text} рублей")
for i in range(len(car_names)):
url = pictures[i].find('img').attrs['src']
filename = f"D:\Games\PyCharm\proj1\\img\{i}.jpg"
wget.download(url, filename)
insert_qwery = f"""INSERT INTO public.Cars_volks(car_name, price, adress, scr)
VALUES ('{car_names[i].text}', '{prices[i].text}', '{adresses[i].text}', '{filename}')""";
cursor.execute(insert_qwery)
connection.commit()
cursor.close()
connection.close()