-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject.py
More file actions
134 lines (105 loc) · 4.88 KB
/
Project.py
File metadata and controls
134 lines (105 loc) · 4.88 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
import json
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import requests
from bs4 import BeautifulSoup
# Парсинг назв та опису (через requests + BeautifulSoup)
def parse_product_details(url):
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
data = {}
title_tag = soup.find('span', class_='cmp-product-details-main__heading-title')
data['назва'] = title_tag.get_text(strip=True) if title_tag else "Невідома назва"
desc_tag = soup.find('div', class_='cmp-product-details-main__description')
data['опис'] = desc_tag.get_text(strip=True) if desc_tag else "Опис відсутній"
return data
# Парсинг нутрієнтів через Selenium
def parse_nutrition_selenium(url):
options = webdriver.ChromeOptions()
options.add_argument("--headless")
driver = webdriver.Chrome(options=options)
try:
driver.get(url)
wait = WebDriverWait(driver, 10)
# Відкрити плашку з нутрієнтами
try:
tab_button = wait.until(EC.element_to_be_clickable(
(By.XPATH, "//button[contains(., 'Енергетична цінність та вміст поживних речовин')]")
))
tab_button.click()
except:
return {}
raw_data = {}
# Основні нутрієнти
try:
ul = wait.until(EC.presence_of_element_located(
(By.CSS_SELECTOR, "ul.cmp-nutrition-summary__heading-primary")
))
details = ul.find_elements(By.CSS_SELECTOR, "li.cmp-nutrition-summary__heading-primary-item")
for item in details:
key = item.find_element(By.CSS_SELECTOR, "span.metric").text.lower().strip()
value = item.find_element(By.CSS_SELECTOR, "span.sr-only.sr-only-pd[aria-hidden='true']").text.strip()
raw_data[key] = value
except:
pass
# Додаткові нутрієнти
try:
div = wait.until(EC.presence_of_element_located(
(By.CSS_SELECTOR, "div.cmp-nutrition-summary__details-column-view-mobile")
))
details = div.find_elements(By.CSS_SELECTOR, "li.label-item")
for item in details:
key = item.find_element(By.CSS_SELECTOR, "span.metric").text.lower().strip()
value = item.find_element(By.CSS_SELECTOR, "span.sr-only").text.strip()
raw_data[key] = value
except:
pass
# Очищення даних
clean_data = {}
for k, v in raw_data.items():
if not k.strip():
continue
clean_key = k.replace(':', '').split('(')[0].strip()
clean_value = v.split('(')[0].strip()
clean_data[clean_key] = clean_value
return clean_data
finally:
driver.quit()
# Збір всіх продуктів з меню
def get_all_product_links(menu_url):
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get(menu_url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
product_links = []
for a in soup.select("a[href*='/product/']"):
href = a['href']
if href.startswith('/ua/uk-ua/product/') and href not in product_links:
product_links.append("https://www.mcdonalds.com" + href)
return list(set(product_links))
def scrape_mcdonalds_data():
base_url = "https://www.mcdonalds.com/ua/uk-ua/eat/fullmenu.html"
product_urls = get_all_product_links(base_url)
print(f"Знайдено {len(product_urls)} посилань")
all_data = {}
for i, url in enumerate(product_urls, 1):
print(f"[{i}/{len(product_urls)}] Обробка: {url}")
try:
info = parse_product_details(url)
nutrition = parse_nutrition_selenium(url)
product_name = info.get('назва', f'Продукт_{i}')
all_data[product_name] = {
"опис": info.get('опис', ''),
"нутрієнти": nutrition
}
except Exception as e:
print(f"Помилка для {url}: {e}")
time.sleep(1)
with open("mcdonalds_menu_data.json", "w", encoding="utf-8") as f:
json.dump(all_data, f, ensure_ascii=False, indent=2)
print("Дані збережено у mcdonalds_menu_data.json")
if __name__ == "__main__":
scrape_mcdonalds_data()