-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdodo_parse.py
164 lines (124 loc) · 5.67 KB
/
dodo_parse.py
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
from selenium.webdriver import Firefox
from fake_useragent import UserAgent
from selenium.webdriver.firefox.options import Options
from bs4 import BeautifulSoup
import time
import os
url = "https://dodopizza.kz/petropavlovsk"
html_file = "saved_page.html"
def get_page():
chrome_options = Options()
print("step one pass")
chrome_options.add_argument("-headless")
print("step two pass")
driver = Firefox(executable_path="/snap/bin/geckodriver", options=chrome_options)
print("step three pass")
driver.get(url)
time.sleep(5)
with open(html_file, "w", encoding="utf-8") as w:
w.write(driver.page_source)
print("page loaded")
driver.close()
get_page()
def try_get_page():
try:
current_time = time.time()
get_creation_time = os.stat(html_file).st_mtime
if current_time - get_creation_time >= 3600:
return "Идет запрос до сервера", get_page()
else:
return "Запрос успешен!"
except FileExistsError:
print("File does`nt exist")
def main(product_type, ingredience="С мясом"):
def read_menu():
with open(html_file, "r", encoding="utf-8") as file:
src = file.read()
soup = BeautifulSoup(src, "lxml")
drinks = "drinks"
title = []
description = []
drinks_title = []
drinks_description = []
avilability_list_food = []
avilability_list_drinks = []
for titles in soup.find("section", class_="sc-1n2d0ov-2 bxiXBh",
attrs={"id": product_type}).find_all("div", class_="sc-1tpn8pe-1 jbQjVh"):
title.append(titles.text)
for desk in soup.find("section", class_="sc-1n2d0ov-2 bxiXBh",
attrs={"id": product_type}).find_all("main", class_="sc-1tpn8pe-0 jDJosZ"):
description.append(desk.text.strip("\n"))
for drinks_titles in soup.find("section", class_="sc-1n2d0ov-2 bxiXBh",
attrs={"id": drinks}).find_all("div", class_="sc-1tpn8pe-1 jbQjVh"):
drinks_title.append(drinks_titles.text)
for drinks_desc in soup.find("section", class_="sc-1n2d0ov-2 bxiXBh",
attrs={"id": drinks}).find_all("main", class_="sc-1tpn8pe-0 jDJosZ"):
drinks_description.append(drinks_desc.text)
for avilability_drinks in soup.find("section", class_="sc-1n2d0ov-2 bxiXBh", attrs={"id": drinks}):
for button in avilability_drinks.find_all("button", class_="sc-1rmt3mq-0 jFZvXc product-control"):
if button.text == "В корзину":
avilability_list_drinks.append("В наличии")
elif button.text == "Будет позже":
avilability_list_drinks.append("Временно отсутствует")
for avilability in soup.find("section", class_="sc-1n2d0ov-2 bxiXBh", attrs={"id": product_type}):
for button in avilability.find_all("button", class_="sc-1rmt3mq-0 jFZvXc product-control"):
if button.text == "Собрать" or button.text == "Выбрать" or button.text == "В корзину":
avilability_list_food.append("В наличии")
elif button.text == "Будет позже":
avilability_list_food.append("Временно отсутствует")
new_list = []
for desk, avail in zip(description, avilability_list_food):
new_list.append(f"{desk} @{avail}@\n")
complite_drinks_list = []
for titles, avail in zip(drinks_title, avilability_list_drinks):
complite_drinks_list.append(f"{titles} @{avail}@\n")
cut_description = []
n = 0
while n < len(new_list):
for item in title:
cut_description.append(new_list[n][len(item):])
n += 1
full_menu = dict(zip(title, cut_description))
return cut_description, full_menu, complite_drinks_list, avilability_list_food
def sort_menu():
full_menu = read_menu()
meat_menu = []
vegan_menu = full_menu[0].copy()
forbiden_words = ["вет", "цып", "инд", "индейки", "чор", "гов", "говядина", "пастр", "кур", "курица", "крыл"]
for elem in full_menu[0]:
for n in forbiden_words:
if n in elem.lower().replace(",", ""):
meat_menu.append(elem)
break
for elem in meat_menu:
if elem in vegan_menu:
vegan_menu.remove(elem)
return vegan_menu, meat_menu
sort_menu()
def get_key(val):
full_menu = read_menu()
for key, value in full_menu[1].items():
if val == value:
return key
return "key doesn't exist"
def print_request_menu():
request_menu = sort_menu()
drinks_title = read_menu()
result = ""
if ingredience == "Без мяса":
for item in request_menu[0]:
result += f"\n {get_key(item)}-{item}"
return result
elif ingredience == "С мясом":
for item in request_menu[1]:
result += f"\n {get_key(item)}-{item}"
return result
elif ingredience == "Напитки":
for drinks in drinks_title[2]:
result += f"{drinks}"
return result
elif ingredience == "Десерты":
for item in request_menu[0]:
result += f"\n {get_key(item)}-{item}"
return result
return print_request_menu()