-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofil_scraping.py
More file actions
139 lines (98 loc) · 3.26 KB
/
profil_scraping.py
File metadata and controls
139 lines (98 loc) · 3.26 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
import json
import time
from selenium.webdriver.chrome.options import Options
import glob
from bs4 import BeautifulSoup as bs
import os
import requests
import selenium
#%% get html
def get_questions_html(url, sleep_time):
print( 'Loading questions page...')
chromeOptions = Options()
chromeOptions.headless = True
driver = selenium.webdriver.Chrome('/home/stephan/chromedriver', options=chromeOptions)
driver.get(url)
"""
for i in range(1, times_to_scroll):
driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
time.sleep(sleep_time)
"""
time.sleep(sleep_time)
return driver.page_source
#%% Load Data
with open("Europe_Usa_cleaned.json") as f:
data = json.load(f)
#%%
p_a = path +"/UltraInstinctMoneyPrinting/profil/"
len(p_a)
#%% Clean data
data = {d : data[d] for d in data if 'finanzen_net' in data[d]}
already = [d[48:] for d in glob.glob(p_a+"*")]
data = {k : v for k, v in data.items() if k not in already}
#%% Get Pages
len(data)
#%% Get Pages
prefix = "https://www.finanzen.net/unternehmensprofil/"
names = [data[d]['finanzen_net'].replace('-aktie','') for d in data]
#%% Symbols
symbol = [d for d in data]
#%% Show Names
names
#%% Urls
url = [prefix+n for n in names]
#pages = [requests.get(u).text for u in url]
#%%
text = [get_questions_html(u, 0) for u in url]
#%%
for u, s in zip(url, symbol):
text = get_questions_html(u, 0)
boxes = bs(text).find_all('div', {'class' : 'box'})
right_boxes = [b for b in boxes if b.find('h2', {'class' : 'box-headline'})]
right_n_boxes = [b.find('h2', {'class' : 'box-headline'}).text for b in right_boxes]
r_b = {n : b.find('tbody') for n, b in zip(right_n_boxes, right_boxes)}
results = {}
for b in r_b:
try:
tr = r_b[b].find_all('tr')
except:
print(r_b[b])
continue
td = [t.find_all('td') for t in tr]
if 'Aktionärsstruktur' in b or 'Bilanz' in b or 'Personal' in b:
results[b] = {tdd[0].text : [t.text for t in tdd[1:]] for tdd in td}.copy()
if 'Management' in b:
try:
text = [str(t).split('<br/>') for t in td]
entries = [(e[1].replace('</td>]', ''), e[0].replace('[<td>', '')) for e in text]
results[b] = {pos : names for pos, names in entries}
except:
pass
if 'Adresse' in b:
new_add = {}
new_add['address'] = td[0][0].text
new_add.update({tdd[0].text : [t.text for t in tdd[1:]] for tdd in td[1:]}.copy())
results[b] = new_add
with open(path + "/UltraInstinctMoneyPrinting/profil/" + s, "w") as f:
print(results)
json.dump(results, f)
#%% Integrating results
already = {d[48:] : d for d in glob.glob(p_a+"*")}
with open(path + "/UltraInstinctMoneyPrinting/Europe_Usa_cleaned.json") as f:
data = json.load(f)
for d in data:
try:
with open(already[d]) as f:
alr = json.load(f)
data[d].update(alr)
except:
print(d)
continue
#%%
data
#%%
with open(path + "/UltraInstinctMoneyPrinting/Europe_Usa_cleaned_profil.json", "w") as f:
json.dump(data, f)
#%%
unkown = [d for d in data if "Adresse" not in data[d]]
len(unkown )