-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
221 lines (161 loc) · 7.79 KB
/
parser.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
import time
import itertools
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
import json
import threading
def xpath_soup(element):
components = []
child = element if element.name else element.parent
for parent in child.parents:
previous = itertools.islice(parent.children, 0, parent.contents.index(child))
xpath_tag = child.name
xpath_index = sum(1 for i in previous if i.name == xpath_tag) + 1
components.append(xpath_tag if xpath_index == 1 else '%s[%d]' % (xpath_tag, xpath_index))
child = parent
components.reverse()
return '/%s' % '/'.join(components)
class AbstractParser():
def __init__(self, city, product_name, url):
self.returned_data_json ={
"market": "dns",
"name" : product_name,
"is_available" : True,
"price" : "0"
}
self.url = url
self.city = city
self.product_name = product_name
self.driver = webdriver.Chrome(ChromeDriverManager().install())
self.driver.get(self.url)
self.driver.maximize_window()
WebDriverWait(self.driver, 20)
self.soup = BeautifulSoup(self.driver.page_source, 'html.parser')
def send_data_to_site(self):
pass
def to_xpath(self, name):
xpath = xpath_soup(name)
return self.driver.find_element_by_xpath(xpath)
def read_data(self):
pass
def click_element(self , element_to_find, parametr , paramatr_value ):
soup_element = self.soup.find(element_to_find, {parametr: paramatr_value})
selenium_path_element = self.to_xpath(soup_element)
ActionChains(self.driver).move_to_element(selenium_path_element).click().perform()
def insert_data_to_element(self , parametr , paramatr_value , inserted_value ):
soup_element = self.soup.find("input", {parametr: paramatr_value})
selenium_path_element = self.to_xpath(soup_element)
ActionChains(self.driver).move_to_element(selenium_path_element).click().send_keys(inserted_value).send_keys(
Keys.ENTER).perform()
def parse_site(self):
self.send_data_to_site()
self.read_data()
def update_soup(self , time_to_wait):
time.sleep(time_to_wait)
self.soup = BeautifulSoup(self.driver.page_source , 'html.parser')
def raw_text_to_send_num(self, text_raw):
text = ""
for i in range(len(text_raw)):
if (text_raw[i].isnumeric()):
text += text_raw[i]
return text
def parse(self):
self.send_data_to_site()
self.read_data()
class ParserDNS(AbstractParser):
def __init__(self, city, product_name):
temp_str = product_name
url = "https://www.dns-shop.ru/" + "search/?q=" + product_name.replace(' ', '+') + "&order=price-asc&stock=soft"
product_name = temp_str
AbstractParser.__init__(self, city, product_name, url)
def send_data_to_site(self):
time.sleep(2)
self.click_element("a" , "class" , "w-choose-city-widget pseudo-link pull-right")
self.update_soup(2)
city_soup = self.soup.find("input", {"data-role": "search-city"})
selenium_path_element = self.to_xpath(city_soup)
ActionChains(self.driver).move_to_element(selenium_path_element).click().send_keys(self.city).perform()
self.update_soup(3)
all_data_cities = self.soup.find("ul" , {"class" : "cities-search"})
all_data_cities = all_data_cities.findAll("span" )
for i in range(len(all_data_cities)):
t = all_data_cities[i].text
if (t.lower() == self.city):
data_city_selenium = self.to_xpath(all_data_cities[i])
ActionChains(self.driver).move_to_element(data_city_selenium).click().perform()
break
def read_data(self):
self.update_soup(3)
is_available_soup = self.soup.find("span" , {"class" : "available"})
if (is_available_soup == None or is_available_soup.text != "В наличии: "):
self.returned_data_json["is_available"] = False
return 0
price_soup = self.soup.find("div", {"class": "product-buy__price"})
price_text_raw = price_soup.text
price_text = self.raw_text_to_send_num(price_text_raw)
self.returned_data_json["price"] = price_text
class ParserRegard(AbstractParser):
def __init__(self, city, product_name ):
AbstractParser.__init__(self, city, product_name, "https://www.regard.ru/")
self.returned_data_json["market"] = "regard"
def send_data_to_site(self):
time.sleep(2)
self.insert_data_to_element("id" , "query" , self.product_name)
self.update_soup(2)
russia = self.soup.find("span", {"class" : "filter-text"})
if (russia != None):
russia = russia.find("a")
russia_selenium = self.to_xpath(russia)
ActionChains(self.driver).move_to_element(russia_selenium).click().perform()
self.click_element("a" , "onclick" , "sorting('price_asc')")
def read_data(self):
self.update_soup(1)
price_div_soup = self.soup.find("div" , {"class" :"price"})
if (price_div_soup == None):
self.returned_data_json["is_available"] = False
return
price_text_raw = price_div_soup.findAll("span")[1]
price_text_raw = price_text_raw.text
price_text = self.raw_text_to_send_num(price_text_raw)
self.returned_data_json["price"] = price_text
class ParserCitilink(AbstractParser):
def __init__(self, city, product_name, url ):
AbstractParser.__init__(self, city, product_name, url)
def send_data_to_site(self):
button_city_soup = self.soup.find("button" , {"class" : "js--CitiesSearch-trigger MainHeader__open-text TextWithIcon"})
button_city_selenium = self.to_xpath(button_city_soup)
ActionChains(self.driver).move_to_element(button_city_selenium).click().perform()
time.sleep(2)
self.soup = BeautifulSoup(self.driver.page_source , 'html.parser')
input_city_soup = self.soup.find("input" , {"name" :"search_text" })
input_city_selenium = self.to_xpath(input_city_soup)
ActionChains(self.driver).move_to_element(input_city_selenium).click().send_keys(self.city).perform()
time.sleep(2)
self.soup = BeautifulSoup(self.driver.page_source , 'html.parser')
click_city_soup = self.soup.find("span" , {"class" :"CitiesSearch__highlight" })
click_city_selenium = self.to_xpath(click_city_soup)
ActionChains(self.driver).move_to_element(click_city_selenium).click().perform()
with open ('data.json' , encoding = 'utf-8') as f:
all_data = json.load(f )
data_to_send = {}
for key in all_data.keys():
data_to_send[key] = data_to_send.get(key , [])
for i in range(len(all_data[key])):
parser_DNS = ParserDNS(key , all_data[key][i])
parser_REGARD = ParserRegard(key , all_data[key][i])
thread_dns = threading.Thread(target = parser_DNS.parse_site())
thread_regard = threading.Thread(target = parser_REGARD.parse_site())
thread_dns.start()
thread_regard.start()
thread_dns.join()
thread_regard.join()
if (parser_DNS.returned_data_json["is_available"]):
data_to_send[key].append(parser_DNS.returned_data_json)
if (parser_REGARD.returned_data_json["is_available"]):
data_to_send[key].append(parser_REGARD.returned_data_json)
with open('sendedData.json' , 'w' , encoding = "utf-8") as f:
json.dump(data_to_send , f , ensure_ascii = False)