-
Notifications
You must be signed in to change notification settings - Fork 0
/
selenium_fiche_thread.py
161 lines (141 loc) · 6.66 KB
/
selenium_fiche_thread.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
#!/usr/bin/python
import threading
import time
import time
import os
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from bs4 import BeautifulSoup
from datetime import datetime
class myThread (threading.Thread):
def __init__(self, threadID):
threading.Thread.__init__(self)
self.threadID = threadID
self.file_name="data_{}.csv".format(str(self.threadID))
def run(self):
print("debut thread fiche {} : {}".format(self.threadID,datetime.now()))
self.file_name="data_{}.csv".format(str(self.threadID))
# with open(self.file_name,'w+') as writer:
# writer.write("morale/physique,nom,lien,crcc,cp,ville,adresse,tel,fax,mail,insciption,numero_inscription,site_web,forme_juridique,agree_par,reseau,compagnie_regionale,president,vice_president,vice_president_delegue,secretaire,tresorier,membre_bureau,autre_membre_conseil_regional,president_honoraires\n")
init_parse(self.file_name)
print("fin thread fiche {} : {}".format(self.threadID,datetime.now()))
def get_driver(url):
options = Options()
options.add_argument("--headless")
options.log.level = 'error'
driver = webdriver.Firefox(firefox_options=options,executable_path=r'geckodriver.exe')
#driver = webdriver.Firefox(executable_path=r'geckodriver.exe')
driver.get(url)
return driver
def init_parse(file_name):
firefox_driver = get_driver("http://www.google.fr")
with open(file_name,'r') as reader:
lines=reader.readlines()
lines.pop(0)
#print('nb lignes',len(lines))
for line in lines:
line_tab=line.split(",")
is_moral= line_tab[0]
#print("ismoral",is_moral,"lien",line_tab[2])
with open("avec_fiche_"+file_name,'a+') as writer:
if is_moral == "personne morale":
writer.write("{},{}\n".format(line.replace("\n",""),get_fiche_morale(firefox_driver,line_tab[2])))
else:
writer.write("{},{}\n".format(line.replace("\n",""),get_fiche_physique(firefox_driver,line_tab[2])))
firefox_driver.close()
def get_fiche_morale(driver,url):
try:
driver.get(url)
page_text = (driver.page_source).encode('utf-8')
soup = BeautifulSoup(page_text, features="lxml")
tab = driver.find_elements_by_xpath("""//*[@id="main"]/div[3]/div[5]/table/tbody/tr""")
valeur=""
tab.pop(0)
for row in tab:
if ">> Acc" in row.find_elements_by_tag_name("td")[0].text:
lien = row.find_elements_by_tag_name("td")[0].find_elements_by_tag_name("a")[0].get_attribute("href")
compagnie=row.find_elements_by_tag_name("td")[0].text.split(" >> Acc")[0]
valeur="{},{}".format(valeur,lien)
#valeur="{},{},{}".format(valeur,compagnie,get_fiche_compagnie_regionale(driver,lien))
else:
valeur="{},{}".format(valeur,row.find_elements_by_tag_name("td")[0].text.replace("\n"," ").replace(","," "))
#print(valeur)
return valeur[1:]
except Exception as e:
print(e)
pass
def get_fiche_physique(driver,url):
#,adresse,tel,fax,mail,insciption,numero_inscription,site_web,forme_juridique,agree_par,reseau,compagnie_regionale
try:
driver.get(url)
page_text = (driver.page_source).encode('utf-8')
soup = BeautifulSoup(page_text, features="lxml")
tab = driver.find_elements_by_xpath("""//*[@id="main"]/div[3]/div[5]/table/tbody/tr""")
valeur=""
tab.pop(0)
for row in tab:
if ">> Acc" in row.find_elements_by_tag_name("td")[0].text:
lien = row.find_elements_by_tag_name("td")[0].find_elements_by_tag_name("a")[0].get_attribute("href")
compagnie=row.find_elements_by_tag_name("td")[0].text.split(" >> Acc")[0]
valeur="{},,,{}".format(valeur,lien)
#valeur="{},{},{}".format(valeur,compagnie,get_fiche_compagnie_regionale(driver,lien))
else:
valeur="{},{}".format(valeur,row.find_elements_by_tag_name("td")[0].text.replace("\n"," ").replace(","," "))
#print(valeur)
return valeur[1:]
except Exception as e:
print(e)
pass
def get_fiche_compagnie_regionale(driver,url):
driver.get(url)
page_text = (driver.page_source).encode('utf-8')
soup = BeautifulSoup(page_text, features="lxml")
tab = driver.find_elements_by_xpath("""//*[@id="main"]/div[3]/div[6]/table[1]/tbody/tr""")
valeur=""
tab.pop(0)
for row in tab:
if ">> Acc" in row.find_elements_by_tag_name("td")[0].text:
lien = row.find_elements_by_tag_name("td")[0].find_elements_by_tag_name("a")[0].get_attribute("href")
valeur="{},{}".format(valeur,get_fiche_compagnie_regionale(driver,lien))
else:
valeur="{},{}".format(valeur,row.find_elements_by_tag_name("td")[0].text.replace("\n"," ").replace(","," "))
#print(valeur)
return valeur[1:]
def fusion():
with open("annuaire.csv",'w+') as writer:
writer.write("morale/physique,nom,lien,crcc,cp,ville,adresse,tel,fax,mail,insciption,numero_inscription,site_web,forme_juridique,agree_par,reseau,compagnie_regionale,president,vice_president,vice_president_delegue,secretaire,tresorier,membre_bureau,autre_membre_conseil_regional,president_honoraires\n")
for i in range(2,5):
with open("avec_fiche_data_{}.csv".format(i),'r') as reader:
lines=reader.readlines()
with open("annuaire.csv",'a') as writer:
writer.writelines(lines)
def get_fiches():
print("debut fiche : {}".format(datetime.now()))
threads = []
# Create new threads
thread1 = myThread(threadID=1)
thread2 = myThread(threadID=2)
thread3 = myThread(threadID=3)
thread4 = myThread(threadID=4)
threads.append(thread1)
threads.append(thread2)
threads.append(thread3)
threads.append(thread4)
# Start all threads
for x in threads:
x.start()
# Wait for all of them to finish
for x in threads:
x.join()
print("debut fusion : {}".format(datetime.now()))
fusion()
print("fin fusion : {}".format(datetime.now()))
for fname in os.listdir("./"):
if "data" in fname:
os.remove(os.path.join("./", fname))
print( "Exiting Main Thread fiche ")
print("fin fiche : {}".format(datetime.now()))