-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser_data.py
More file actions
190 lines (164 loc) · 7.45 KB
/
parser_data.py
File metadata and controls
190 lines (164 loc) · 7.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from playwright.sync_api import sync_playwright, Page
import pandas as pd
import random
import yaml
import logging
# Настройка логгера
logging.basicConfig(
filename="logs/error_log_data.log", # Путь к файлу лога
level=logging.ERROR, # Уровень логирования (только ошибки)
format="%(asctime)s - %(levelname)s - %(message)s",
encoding="utf-8",
)
# Загрузка конфигурации из YAML
with open("config/config.yaml", "r") as file:
config = yaml.safe_load(file)
paths = config["path"]
user_agents = config["user_agents"]
# Чтение ссылок из CSV файла
try:
links = pd.read_csv(paths)[:10]
except Exception as e:
logging.error(f"Ошибка при чтении файла ссылок: {e}")
def close_baner(page: Page):
"""Закрывает баннер на текущей странице."""
try:
text_element = page.locator("h2")
if text_element.count() > 0:
print(text_element.inner_text(), "Текст найден")
print("Элемент найден!")
close_button = page.get_by_text(
"Продолжать, не поддерживая нас", timeout=60000
)
close_button.click(timeout=60000)
print("Баннер закрыт.")
else:
print("Элемент не найден")
except Exception as e:
print(f"Не удалось нажать кнопку закрытия: {e}")
logging.error(f"Ошибка при закрытии баннера: {e}")
def get_fragrantica_data(page: Page, link: str, df):
"""Собирает данные со страницы Fragrantica."""
page.goto(link, timeout=60000)
# Закрываем всплывающий банер
close_baner(page)
data = {} # Создаем словарь для хранения данных
try:
# Заголовок страницы
locator = page.locator("div#toptop h1")
title_text = locator.inner_text()
data["title"] = title_text
# Основные аккорды
accord_bars = page.locator("div.accord-bar")
for i in range(accord_bars.count()):
text = accord_bars.nth(i).inner_text().strip()
style_attr = accord_bars.nth(i).get_attribute("style")
if style_attr and "width:" in style_attr:
width_value = (
style_attr.split("width:")[1].split(";")[0].strip().replace("%", "")
)
data[text] = width_value
# Голосования
rows = page.locator(
"div[style='display: flex; flex-direction: row; align-items: center;']"
)
vote_buttons = rows.locator(".vote-button-name")
for i in range(rows.count()):
row = rows.nth(i)
text = vote_buttons.nth(i).inner_text()
chart = row.locator("..").locator(".voting-small-chart-size div div")
style_attr = chart.get_attribute("style")
if style_attr and "width:" in style_attr:
width_value = (
style_attr.split("width:")[1].split(";")[0].strip().replace("%", "")
)
data[text] = width_value
# Оценки
rows = page.locator(
"div[style='display: flex; justify-content: space-evenly;']"
).first
name = rows.locator("div[style='display: flex; justify-content: center;']")
evaluations = rows.locator(".voting-small-chart-size div div")
for i in range(name.count()):
text = name.nth(i).inner_text()
chart = evaluations.nth(i).get_attribute("style")
if chart and "width:" in chart:
width_value = (
chart.split("width:")[1].split(";")[0].strip().replace("%", "")
)
data[text] = width_value
# Сезонность
rows = page.locator(
"div[style='display: flex; justify-content: space-evenly;']"
).nth(1)
name = rows.locator("div[style='display: flex; justify-content: center;']")
evaluations = rows.locator(".voting-small-chart-size div div")
for i in range(name.count()):
text = name.nth(i).inner_text()
chart = evaluations.nth(i).get_attribute("style")
if chart and "width:" in chart:
width_value = (
chart.split("width:")[1].split(";")[0].strip().replace("%", "")
)
data[text] = width_value
# Рейтинг
rating = page.locator("span[itemprop='ratingValue']").inner_text()
votes = (
page.locator("span[itemprop='ratingCount']").inner_text().replace(",", "")
)
data["rating"] = rating
data["votes"] = votes
# Пирамида аромата
notes_selector = "div[style='display: flex; justify-content: center; text-align: center; flex-flow: wrap; align-items: flex-end; padding: 0.5rem;']"
notes_elements = page.locator(notes_selector)
notes_names = ["Top Notes", "Middle Notes", "Base Notes"]
for i in range(notes_elements.count()):
notes_text = notes_elements.nth(i).inner_text().split("\n")
data[notes_names[i]] = notes_text
# Отзывы о стойкости
longevity = page.locator("div[class='grid-x grid-margin-x']")
for i in range(2, 6):
vote = longevity.nth(i).inner_text().split()
data[vote[0]] = vote[1]
# Отзывы о шлейфе
sillage = page.locator("div[class='grid-x grid-margin-x']")
for i in range(7, 10):
vote = sillage.nth(i).inner_text().split()
data[vote[0]] = vote[1]
# Отзывы о гендере
gender = page.locator("div[class='grid-x grid-margin-x']")
for i in range(11, 15):
vote = gender.nth(i).inner_text().split()
data[vote[0]] = vote[1]
# Отзывы о соотношении цены и качества
price_value = page.locator("div[class='grid-x grid-margin-x']")
for i in range(16, 20):
vote = price_value.nth(i).inner_text().split()
data[vote[0]] = vote[1]
except Exception as e:
print(f"Ошибка при сборе данных с {link}: {e}")
logging.error(f"Ошибка при сборе данных с {link}: {e}")
return df
df = pd.concat(
[df, pd.DataFrame([data])], ignore_index=True
) # Добавление данных в DataFrame
return df
# Основной блок
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
# Контекст браузера
context = browser.new_context(
viewport={"width": 1280, "height": 720},
user_agent=random.choice(user_agents),
)
df = pd.DataFrame() # Создаем пустой DataFrame
page = context.new_page()
# Итерация по всем ссылкам в файле
for link in links["link"]:
df = get_fragrantica_data(page, link, df)
# Сохранение DataFrame в CSV
df.to_csv("data/fragrance_data.csv", index=False)
print("Данные успешно собраны и сохранены в fragrance_data.csv")
input("Нажмите Enter, чтобы закрыть браузер...")
context.close()
browser.close()