-
Notifications
You must be signed in to change notification settings - Fork 0
/
xrysos_odigos2.py
321 lines (299 loc) · 12.4 KB
/
xrysos_odigos2.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import requests
from time import sleep
import time
from bs4 import BeautifulSoup as bs
import os
import sys
from random import randint
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
import xlsxwriter
def main():
start_time = time.time()
#HERE IT FINDS THE PATH
if getattr(sys, 'frozen', False):
application_path = os.path.dirname(sys.executable)
else:
try:
app_full_path = os.path.realpath(__file__)
application_path = os.path.dirname(app_full_path)
except NameError:
application_path = os.getcwd()
#Here we create the variable that is going to be used to all the functions for the path
path = os.path.join(application_path)
#Here it takes the Chrome Path
chrome_path = (path + "\\chromedriver.exe")
with open(path + "\\link.txt", 'r') as link_file:
link = link_file.read()
options = webdriver.ChromeOptions()
options.headless = False
options.add_experimental_option("excludeSwitches", ["enable-logging"])
browser = webdriver.Chrome(options=options, executable_path=chrome_path)
browser.get(link)
sleep(2)
text = name(browser, link)
question(browser, link, path, text)
sleep(3)
browser.quit()
time_waiting = time.time() - start_time
hours, minutes, seconds = waitingTime(time_waiting)
print("Η διαδικασία ολοκληρώθηκε")
if hours == 1:
ores = 'Ώρα'
else:
ores = 'Ώρες'
print('Ο χρόνος αναμονής ήτανε: ', int(hours), ' ', ores, ' ', int(minutes), ' λεπτά ',
'και ',int(seconds), ' δευτερόλεπτα.', sep='')
#Here it extracts the ulrs
def extract_urls(browser, link, path, text):
header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.192 Safari/537.36 OPR/74.0.3911.218"}
requests.get(link, headers = header)
sleep(5)
print("Αυτή η διαδικασία θα διαρκέσει μερικά λεπτά.\n", "Μην κλείσετε το πρόγραμμα......")
print('' * 2)
last_height = 0
proceed = ''
links = []
while True:
browser.execute_script('window.scrollTo(0, document.body.scrollHeight);')
sleep(1)
#GET THE URLS
elements = browser.find_elements_by_xpath('//a[@content]')
sleep(1)
for elem in elements:
urls = elem.get_attribute('content')
elements = browser.find_elements_by_xpath('//a[@content]')
if urls not in links:
links.append(urls)
print(links)
print('')
sleep(2)
new_height = browser.execute_script("return document.body.scrollHeight")
if new_height == last_height:
try:
#epomeno button
browser.find_element_by_class_name("page_next").click()
sleep(4)
except Exception:
break
last_height = new_height
if False:
proceed = False
else:
proceed = True
sleep(10)
#Create a folder with the name of the label
if proceed == True:
print("Περιμένετε μέχρι να δημιουργήθει ο φάκελος για να περαστούν τα εξαγμένα urls.\nΜην κλείσετε το πρόγραμμα...")
print('' * 2)
sleep(5)
sleep(2)
try:
os.mkdir(path + '\\' + text)
link_extraction = open(path + "\\" + text + '\\extracted_links.txt', 'a')
sleep(2)
print("Το αρχείο extracted_links.txt δημιουργήθηκε.")
print('' * 2)
for i in links:
link_extraction.write(i + '\n')
link_extraction.close()
sleep(2)
print('Τα urls περαστήκανε με επιτυχία.')
print('')
except FileExistsError:
print('Ο φάκελος υπάρχει ήδη.')
print('')
link_extraction = open(path + "\\" + text + '\\extracted_links.txt', 'w')
sleep(2)
print("Το αρχείο extracted_links.txt δημιουργήθηκε.")
print('' * 2)
for i in links:
link_extraction.write(i + '\n')
link_extraction.close()
sleep(2)
print('Τα urls περαστήκανε με επιτυχία.')
print('')
#Here it extracts the information of the urls like: names, addresses, etc
def extract_informations(browser, path, text):
print("Ποιά είναι η πόλη που θέλετε να κάνετε εξαγωγή τα δεδομένα; (π.χ. Λάρισα)")
city = input("Δώσε την ονομασία της πόλης: ")
print(""*2)
ex_f = open(path + "\\" + text + "\\extracted_links.txt", 'r')
URL = ex_f.readline()
URL = URL.rstrip('\n')
count = 0
#EXCEL
workbook = xlsxwriter.Workbook(path + "\\" + text + f"\\{text}.xlsx")
worksheet = workbook.add_worksheet("ΚΑΤΑΛΟΓΟΣ")
bold = workbook.add_format({"bold" : True})
worksheet.write("A1", "ONOMA", bold)
worksheet.write("B1", "ΤΟΠΟΘΕΣΙΑ", bold)
worksheet.write('C1', 'ΤΗΛΕΦΩΝΟ', bold)
extraction = []
while URL != '':
data = requests.get(URL)
print(data)
soup = bs(data.content, 'html.parser')
sleep(randint(10, 15))
try:
#Address
address = soup.find_all('span', {'class':'streetAddressProf'})[0].text
if city in address:
#Name
name = soup.find_all('h1', {'id':'ProfileLabel'})[0].text
#Telephone number
links2 = []
try:
tel1 = soup.find_all('a', {'data-event':'phone1.profile'})[0].text
if tel1 not in links2:
links2.append(tel1)
except Exception:
tel1 = soup.find_all('a', {'data-event':'mobile.profile'})[0].text
if tel1 not in links2:
links2.append(tel1)
#More tel numbers (βρίσκει αν υπάρχει και δευτερο νουμερο τηλεφωνου)
try:
try:
tel2 = soup.find_all('a', {'data-event':'phone2.profile'})[0].text
if tel2 not in links2:
links2.append(tel2)
except Exception:
tel2 = soup.find_all('a', {'data-event':'mobile.profile'})[0].text
if tel2 not in links2:
links2.append(tel2)
count += 1
print('%.f) Όνομα: '%(count), name, '\nΤοποθεσία: ', address, f'\nΤηλέφωνο: {links2}')
print("")
extraction.append(name)
extraction.append(address)
extraction.append(links2)
except Exception:
count += 1
print('%.f) Όνομα: '%(count), name, '\nΤοποθεσία: ', address, f'\nΤηλέφωνο: {links2}')
print("")
extraction.append(name)
extraction.append(address)
extraction.append(links2)
URL = ex_f.readline()
URL = URL.rstrip('\n')
else:
URL = ex_f.readline()
URL = URL.rstrip('\n')
except Exception as Err:
print(Err)
print()
URL = ex_f.readline()
URL = URL.rstrip('\n')
ex_f.close()
count = 0
True_1 = False
True_2 = False
#Here it pass the content of the list to Excel and .txt files
with open(path + "\\" + text + f"\\{text}.txt", "w") as text_f:
col_count = 0
for i in extraction:
count += 1
if count == 3:
try:
if True_1 == False:
i_1 = i[0][0:10]
i_2 = i[1][0:10]
text_f.write(str(i_1) + ", ")
text_f.write(str(i_2) + "\n")
if col_count == 0:
worksheet.write(f"C{col_count+2}", str(i_1))
worksheet.write(f"C{col_count + 3}", str(i_2))
True_1 = True
True_2 = False
col_count += 2
else:
worksheet.write(f"C{col_count+1}", str(i_1))
worksheet.write(f"C{col_count + 2}", str(i_2))
True_1 = True
True_2 = False
col_count += 1
else:
i_1 = i[0][0:10]
i_2 = i[1][0:10]
text_f.write(str(i_1) + ", ")
text_f.write(str(i_2) + "\n")
worksheet.write(f"C{col_count + 2}", str(i_1))
worksheet.write(f"C{col_count + 3}", str(i_2))
True_1 = True
True_2 = False
col_count += 2
except Exception:
if i == []:
i_1 = "-"
else:
i_1 = i[0][0:10]
text_f.write(str(i_1) + "\n")
if True_2 == False:
worksheet.write(f"C{col_count+2}", str(i_1))
col_count += 2
else:
worksheet.write(f"C{col_count+1}", str(i_1))
col_count += 1
True_1 = False
True_2 = True
count = 0
elif count == 2:
if True_1 == True:
i = str(i)
i = i.strip("['")
i = i.strip("']")
text_f.write(str(i) + "\n")
worksheet.write(f"B{col_count + 2}", str(i))
else:
i = str(i)
i = i.strip("['")
i = i.strip("']")
text_f.write(str(i) + "\n")
if True_2 != True:
worksheet.write(f"B{col_count+2}", str(i))
else:
worksheet.write(f"B{col_count+1}", str(i))
else:
if True_1 == True:
i = str(i)
i = i.strip("['")
i = i.strip("']")
text_f.write(str(i) + "\n")
worksheet.write(f"A{col_count + 2}", str(i))
else:
i = str(i)
i = i.strip("['")
i = i.strip("']")
text_f.write(str(i) + "\n")
if True_2 != True:
worksheet.write(f"A{col_count+2}", str(i))
else:
worksheet.write(f"A{col_count+1}", str(i))
workbook.close()
def question(browser, link, path, text):
print("Ποιά διαδικασία θες να εκτελέσεις;\nΓια εξαγωγή των urls δώσε --> 1\nΓια εξαγωγή πληροφοριών από τα "
"urls δώσε --> 2")
print('')
answer = int(input("Δώσε την επιλογή σου: "))
print('')
while answer < 1 or answer > 2:
print("Λάθος επιλογή. Ξανά προσπάθησε.")
answer = int(input("Δώσε την επιλογή σου: "))
print('')
if answer == 1:
extract_urls(browser, link, path, text)
else:
extract_informations(browser, path, text)
def name(browser, link):
name_ = browser.find_element_by_xpath("""//*[@id="MainSearchContainer"]/div/div[1]/div[1]/h1""")
text = name_.text
return text
def waitingTime(time_waiting):
ores = time_waiting // 3600
lepta = (time_waiting // 60) % 60
defterolepta = time_waiting % 60
return ores, lepta, defterolepta
main()