-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuronext.py
More file actions
432 lines (336 loc) · 17.2 KB
/
euronext.py
File metadata and controls
432 lines (336 loc) · 17.2 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import re
import time
import requests
from lxml.html import document_fromstring
from browser import Browser
class Euronext:
def __init__(self):
self.browser = Browser()
self.headers = self.GetHeaders()
self.languages = set(['bg', 'cs', 'da', 'de', 'nl', 'el', 'et', 'fi', 'fr', 'hr', 'hu', 'is', 'it',
'lv', 'lt', 'lb', 'mt', 'no', 'pl', 'pt', 'ro', 'sk', 'sl', 'es', 'sv'])
self.language_regex, self.page_regex, self.file_regex = self.GetRegexes()
def __del__(self):
self.browser.Quit()
def GetHeaders(self):
return {'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,'
'*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'accept-language': 'en-US;q=0.8,en;q=0.7',
'cache-control': 'max-age=0',
'sec-ch-ua': '" Not;A Brand";v="99", "Google Chrome";v="97", "Chromium";v="97"',
'sec-ch-ua-mobile': '?1',
'sec-ch-ua-platform': 'Android',
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'none',
'sec-fetch-user': '?1',
'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) '
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Mobile Safari/537.36'}
def GetRegexes(self):
language_regex = r'(?<=\/)(%s)(?=\/|$)' % '|'.join(self.languages)
language_regex = re.compile(language_regex, re.IGNORECASE)
page_regex = r'invest|finan[sc]|regulat|réglement|document'
page_regex = re.compile(page_regex, re.IGNORECASE)
file_regex = r'statement|report|filing|result|déclaration|rapport|résultat|jaarverslag|(?:reference|registration)' \
r'[\s_-]*document|document[\s_-]*(?:de|d’)?[\s_-]*(?:référence|d’enregistrement|reference)' \
r'|(?:hy|fy|q\d|ra|rs|rfa|rfs|udr|urd)[\s_-]*(?:19|20)\d{2}'
file_regex = re.compile(file_regex, re.IGNORECASE)
return language_regex, page_regex, file_regex
def ExtractCompanies(self, html):
root = document_fromstring(html)
rows = root.cssselect('table[id="stocks-data-table-es"] tbody tr')
companies = []
for row in rows:
columns = row.cssselect('td')
texts = [column.text_content().strip() for column in columns[1:5]]
if all(texts):
registrant_name, isin, symbol, market = texts
quotes_link = columns[1].cssselect('a')[0]
quotes_url = 'https://live.euronext.com%s' % quotes_link.attrib['href']
match = re.search(r'^.+equities\/[^\/]+\/', quotes_url)
if match is not None:
info_url = match.group() + 'company-information'
companies.append((symbol, isin, registrant_name, market, info_url))
return companies
def GetCompanies(self):
self.browser.LoadPage('https://live.euronext.com/en/products/equities/list')
accept_button = self.browser.WaitForElement('div[id="popup-buttons"] button', timeout=10)
if accept_button is not None:
accept_button.click()
time.sleep(1)
companies, last_company = set(), None
while 1:
self.browser.WaitForElement('table[id="stocks-data-table-es"] tbody tr')
new_companies = self.ExtractCompanies(self.browser.PageSource())
if new_companies[-1] == last_company:
time.sleep(1)
continue
last_company = new_companies[-1]
companies |= set(new_companies)
next_button = self.browser.GetElement('a[id="stocks-data-table-es_next"]')
class_name = next_button.get_attribute('class')
if 'disabled' in class_name:
companies = sorted(companies, key=lambda company: company[2])
break
self.browser.ScrollIntoView(next_button)
time.sleep(1)
next_button = self.browser.GetElement('a[id="stocks-data-table-es_next"]')
next_button.click()
time.sleep(1)
return companies
def GetCompanyInfo(self, info_url):
if self.browser.Url() != info_url:
self.browser.LoadPage(info_url)
description_element = self.browser.WaitForElement('p[class^="address__text"]')
market_full_name = description_element.text.split('\n')[0].strip() if description_element else None
address_elements = self.browser.GetElement('address[id] > div:nth-of-type(1) > div:not(:first-of-type)', multiple=True)
if len(address_elements) >= 3:
address_line = address_elements[0].text.strip()
address_city = address_elements[-2].text.strip()
address_country = address_elements[-1].text.strip()
else:
address_line, address_city, address_country = None, None, None
phone_element = self.browser.GetElement('address[id] a[href^="tel"]')
phone_number = phone_element.text.replace(' ', '').strip() if phone_element else None
website_element = self.browser.GetElement('address[id] a[href^="http"]')
website = website_element.text.strip() if website_element else None
return market_full_name, address_line, address_city, address_country, phone_number, website
def ExtractWebsite(self):
links = self.browser.GetElement('a[data-toggle]', multiple=True)
for link in links:
if not link.is_displayed():
continue
link.click()
dialog = self.browser.WaitForElement('div[id="block-company-press-releases-block-off-canvas"] div[role="document"]',
timeout=10)
if dialog is not None:
text = dialog.text.strip()
match = re.search(r'\w+\.(?:com|net|org)', text)
if match is not None:
website = 'https://%s' % match.group()
return website
close_buttons = self.browser.GetElement('button[data-dismiss]', multiple=True)
for close_button in close_buttons:
if close_button.is_displayed():
close_button.click()
break
return None
def GetEnglishUrl(self, url):
english_url = self.language_regex.sub('en', url)
if english_url != url:
response = requests.get(english_url, headers=self.headers)
if response.status_code == 200:
return english_url
return None
def GetUrls(self):
url = self.browser.Url()
html = self.browser.PageSource()
root = document_fromstring(html)
root.make_links_absolute(url)
links = root.cssselect('a[href]')
invalid_start_regex = re.compile(r'^(?:javascript|mailto)', re.IGNORECASE)
urls = set()
for link in links:
url = link.attrib['href']
if url and not invalid_start_regex.search(url):
text = link.text_content().strip()
urls.add((url, text))
return urls
def ExtractPageUrls(self, page_url):
components = requests.utils.urlparse(page_url)
path = components.path.strip('/')
is_home_page = path == '' or path in self.languages
page_url = self.GetEnglishUrl(page_url) or page_url
page_url = page_url.strip('/')
if not self.browser.LoadPage(page_url):
return set()
self.browser.WaitForElement('body')
urls = self.GetUrls()
page_urls = set([] if is_home_page else [page_url])
for url, text in urls:
if url and re.search(r'\.html?|\/[^.]+$', url):
if (not is_home_page and url.startswith(page_url)) or self.page_regex.search(url) or self.page_regex.search(text):
url = re.split(r'#|\?', url)[0].strip('/')
page_urls.add(url)
slash_regex = re.compile(r'(?<!:)\/\/')
for url in list(page_urls):
url_without_prefix = slash_regex.sub('/', self.language_regex.sub('', url))
if url_without_prefix != url and url_without_prefix in page_urls:
page_urls.discard(url)
return page_urls
def GetPageUrls(self, info_url):
if self.browser.Url() != info_url:
self.browser.LoadPage(info_url)
page_link = self.browser.WaitForElement('address a[target="_blank"]')
if page_link is None:
email_link = self.browser.GetElement('a[href^="mailto"]')
if email_link is None:
page_url = self.ExtractWebsite()
else:
email_url = email_link.get_attribute('href')
page_url = 'https://%s' % email_url.split('@')[-1]
else:
page_url = page_link.get_attribute('href')
if not page_url.startswith('http'):
page_url = 'https://%s' % page_url
if page_url is None:
page_urls = set()
else:
page_urls = self.ExtractPageUrls(page_url)
return list(page_urls)
def GetUniqueSelector(self, element):
path = []
while 1:
parent = element.getparent()
if parent is None:
break
tag = element.tag
siblings = [child for child in parent.getchildren() if child.tag == tag]
if len(siblings) > 1:
idx = siblings.index(element)
tag += ':nth-of-type(%d)' % (idx + 1)
path.append(tag)
element = parent
path = path[::-1]
selector = ' > '.join(path).lower()
return selector
def FindLists(self):
html = self.browser.PageSource()
root = document_fromstring(html)
list_item_selectors = ['ul > li', 'ol > li', 'div > a', 'div > div']
list_items_map = {}
for list_item_selector in list_item_selectors:
lists_items = root.cssselect(list_item_selector)
for list_item in lists_items:
list = list_item.getparent()
if list in list_items_map:
list_items_map[list].append(list_item)
else:
list_items_map[list] = [list_item]
selectors = []
for list_items in list_items_map.values():
if len(list_items) < 2:
continue
texts = [list_item.text_content().strip() for list_item in list_items]
number_texts = [text for text in texts if text.isdigit()]
number_ratio = len(number_texts) / len(texts)
if number_ratio >= 0.5:
unique_selector = self.GetUniqueSelector(list_items[0])
unique_selector = unique_selector.rsplit(':nth-of-type', maxsplit=1)[0]
selectors = [selector for selector in selectors if selector not in unique_selector]
if all(unique_selector not in selector for selector in selectors):
selectors.append(unique_selector)
return selectors
def IterateList(self, list_item_selector):
urls, previous_texts, texts = set(), set(), []
while 1:
list_items = self.browser.GetElement(list_item_selector, multiple=True) or []
if not list_items:
text = next((text for text in texts if text.isdigit() and text not in previous_texts), None)
if text is not None:
tag = list_item_selector.rsplit(' > ', maxsplit=1)[-1]
list_item = self.browser.GetElementByText(text, tag)
if list_item is not None:
list_item_selector = self.browser.GetSelector(list_item)
list_item_selector = list_item_selector.rsplit(':nth-of-type', maxsplit=1)[0]
list_items = self.browser.GetElement(list_item_selector, multiple=True) or []
texts = [list_item.text.strip() for list_item in list_items]
idx = next((idx for idx, text in enumerate(texts) if text.isdigit() and text not in previous_texts), None)
if idx is None:
break
list_item, text = list_items[idx], texts[idx]
previous_texts.add(text)
selector = self.browser.GetSelector(list_item)
self.browser.ScrollIntoView(list_item)
time.sleep(1)
list_item = self.browser.GetElement(selector)
try:
self.browser.RemoveOverlappingElements(list_item)
list_item.click()
time.sleep(1)
urls |= self.GetUrls()
except:
pass
return urls
def CheckLists(self):
urls, list_item_selectors = set(), self.FindLists()
for list_item_selector in list_item_selectors:
list_selector = list_item_selector.rsplit(' > ', maxsplit=1)[0]
list = self.browser.GetElement(list_selector)
if list is None:
continue
list_parent = list.find_element_by_xpath('..')
list_parent_class = list_parent.get_attribute('class')
if 'dropdown' in list_parent_class:
self.browser.SetAttribute(list, 'style', 'display: block !important; opacity: 1 !important')
urls |= self.IterateList(list_item_selector)
return urls
def SelectOptions(self):
urls = set()
options = self.browser.GetElement('select option', multiple=True) or []
option_selectors = [self.browser.GetSelector(option) for option in options[:60]]
for option_selector in option_selectors:
option = self.browser.GetElement(option_selector)
if option is not None:
text = option.text
if re.search(r'(?:19|20)\d{2}', text):
select = option.find_element_by_xpath('..')
self.browser.SelectOption(select, text)
time.sleep(1)
idx = option_selector.rfind('form')
if idx != -1:
input_selector = option_selector[:idx + 4] + ' input[type="submit"]'
input = self.browser.GetElement(input_selector)
if input is not None:
try:
self.browser.ScrollIntoView(input)
time.sleep(1)
self.browser.RemoveOverlappingElements(input)
input.click()
while 1:
time.sleep(1)
disabled = input.get_attribute('disabled')
if disabled is None:
break
except:
pass
urls |= self.GetUrls()
urls |= self.CheckLists()
return urls
def ExtractStatementUrls(self, page_url):
if not self.browser.LoadPage(page_url):
return set()
self.browser.WaitForElement('body')
link = self.browser.GetElement('head > link[rel="alternate"][hreflang="en"]')
if link is not None:
english_url = link.get_attribute('href')
if english_url != page_url and self.browser.LoadPage(english_url):
self.browser.WaitForElement('body')
self.browser.ScrollToBottom()
time.sleep(1)
urls = self.GetUrls()
urls |= self.CheckLists()
urls |= self.SelectOptions()
statement_urls = set()
for url, text in urls:
if url and re.search(r'\.pdf(\?|$)', url):
if self.file_regex.search(url) or self.file_regex.search(text):
statement_urls.add(url)
return statement_urls
def GetStatementUrls(self, info_url):
urls = ['*://*googlesyndication.com/*', '*://*doubleclick.net/*', '*://*googletagmanager.com/*',
'*://*google-analytics.com/*', '*://*cloudflare.com/*', '*://*facebook.net/*',
'*://*cookiebot.com/*', '*://*cookieinformation.com/*', '*://*consentframework.com/*',
'*://*cookielaw.org/*', '*://*privacy-center.org/*']
self.browser.BlockUrls(urls)
try:
page_urls = self.GetPageUrls(info_url)
except:
return []
statement_urls = set()
for page_url in page_urls:
try:
statement_urls |= self.ExtractStatementUrls(page_url)
except:
continue
return list(statement_urls)