-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraping.py
128 lines (100 loc) · 4.91 KB
/
scraping.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
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
def get_list_url():
# list_url = []
set_url = set()
driver = webdriver.Chrome("chromedriver")
driver.implicitly_wait(15)
driver.get("https://myino.app/#/annuaire")
driver.find_element(By.XPATH, '//*[@id="cdk-overlay-0"]/mat-bottom-sheet-container/app-cookies-acceptation-sheet/div/button[2]/span').click()
driver.implicitly_wait(20)
driver.find_element(By.XPATH, '//*[@id="directory"]/app-list/div/app-search-bar/form/button[2]/span').click()
driver.implicitly_wait(20)
for j in range(1,38): # 1 à 37
driver.find_element(By.XPATH, f'//*[@id="mat-chip-list-1"]/div/mat-chip[{j}]').click() # 1 à 37
driver.implicitly_wait(20)
i = 0
last_url = ''
while True:
element = driver.find_element(By.XPATH, f'//*[@id="directory"]/app-list/div/ul/li[1]/a/div[2]/a')
url = element.get_attribute('href')
if last_url == url:
break
last_url = url
set_url.add(url)
print(i+1, url)
i += 1
element_to_scroll = driver.find_element(By.XPATH, f'//*[@id="directory"]/app-list/div/ul/li[2]/a/div[2]/a')
driver.execute_script("arguments[0].scrollIntoView();",
element_to_scroll)
element_to_scroll = driver.find_element(By.XPATH, f'//*[@id="directory"]/app-list/div/ul/li[3]/a/div[2]/a')
driver.execute_script("arguments[0].scrollIntoView();",
element_to_scroll)
time.sleep(2)
time.sleep(5)
driver.close()
return set_url
def write_file(liste):
with open('list_url.txt', 'w') as fp:
for item in liste:
# write each item on a new line
fp.write("%s\n" % item)
def write_csv(liste):
with open('list_.csv', 'w') as fp:
fp.write('Nom,Effectif sur Inovallée,Téléphone\n')
for item in liste:
fp.write(f'{",".join(item)}\n')
def read_file():
liste = []
with open('list_url.txt', 'r') as fp:
for line in fp:
# remove linebreak from a current name
# linebreak is the last character of each line
x = line[:-1]
# add current item to the list
liste.append(x)
return liste
def get_info(list_url):
liste_csv = []
for i, url in enumerate(list_url):
try:
driver = webdriver.Chrome("chromedriver")
driver.implicitly_wait(15)
driver.get(url)
driver.find_element(By.XPATH, '//*[@id="cdk-overlay-0"]/mat-bottom-sheet-container/app-cookies-acceptation-sheet/div/button[2]/span').click()
driver.implicitly_wait(20)
num_section = 0
company_name = driver.find_element(By.XPATH,
'//*[@id="fiche"]/app-header-fiche/header/div/div[2]/h1').text
tel = driver.find_element(By.XPATH,
f'//*[@id="fiche"]/app-info-fiche/div/section[{2+num_section}]/ul/li[1]/a/p').text
info_element = driver.find_element(By.XPATH,
f'//*[@id="fiche"]/app-info-fiche/div/section[{1+num_section}]/ul/li[3]/h3').text
if info_element == "Effectif":
effective_inovalle = driver.find_element(By.XPATH,
f'//*[@id="fiche"]/app-info-fiche/div/section[{1+num_section}]/ul/li[4]/p').text
elif info_element == "Date de départ du Tarmac":
effective_inovalle = driver.find_element(By.XPATH,
f'//*[@id="fiche"]/app-info-fiche/div/section[{1+num_section}]/ul/li[5]/p').text
elif info_element == "Date d'implantation au Tarmac":
effective_inovalle = driver.find_element(By.XPATH,
f'//*[@id="fiche"]/app-info-fiche/div/section[{1+num_section}]/ul/li[6]/p').text
else:
effective_inovalle = driver.find_element(By.XPATH,
f'//*[@id="fiche"]/app-info-fiche/div/section[{1+num_section}]/ul/li[3]/p').text
driver.close()
print(i+1, url, company_name, effective_inovalle, tel)
liste_csv.append([company_name, effective_inovalle, tel])
except Exception as e:
continue
return liste_csv
# list_url = get_list_url()
# write_file(list_url)
list_url = read_file()
liste_csv = get_info(list_url)
write_csv(liste_csv)