-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkand.py
executable file
·122 lines (102 loc) · 2.87 KB
/
kand.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
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import random
import requests
import bs4
import string
import csv
import time
import re
min = 101
max = 101+66
amount = 66
random.seed(123)
#data = random.sample(range(min, max), amount)
data = range(min, max)
j=0
def validate_mobile(value):
rule = re.compile(r'^[0-9 +-,]+$')
return rule.search(value) is not None
def validate_email(value):
rule = re.compile(r'^.+@.+$')
return rule.search(value) is not None
for i in data:
j+=1
#if j>10: break
url=("https://ep2019.valimised.ee/et/candidates/candidate-%d.html") % i
#print(url)
succ = 1
while succ > 0:
try:
page = requests.get(url)
succ = 0
except requests.exceptions.RequestException as e:
print(e)
succ += 1
time.sleep(succ)
soup = bs4.BeautifulSoup(page.content, 'lxml')
#dist = soup.find_all("span", {"class", "breadcrumb-item active"})
#print(dist)
#ringkond = dist[0].text.split(" ")[-1]
candname = soup.find_all("span", {"class", "uppercase"})
nimi = candname[0].text.title()
table = soup.find(name='tbody')
if table is None:
print("%d: " % i)
continue;
rows = table.find_all('tr')
res = [i, nimi]
mob = ""
email = ""
addr = ""
for row in rows:
cols = row.find_all('td')
raw=cols[0].text
# print(raw)
if len(raw)==0:
continue;
# res = [raw, cols[1].text]
if raw.strip()=="Kontaktandmed:":
incell=str(cols[1]).replace("<br/>\n</td>","").replace('<td class="second">',"")
if "<br/>" in incell:
kont = str(cols[1]).split('<br/>')
else:
kont = [cols[1].text.strip()]
#print(kont)
for k in kont:
#print(k)
k = bs4.BeautifulSoup(k, 'lxml').text
#print(k)
if validate_mobile(k):
mob = k.strip()
elif validate_email(k):
email = k.strip()
else:
addr = k.strip()
else:
res.append(cols[1].text.strip())
# for col in cols:
# k+=1
# raw=col.text
# if len(raw)==0 or raw in ["Andmed"]:
# break;
# print(raw)
# if k==2: break
#print(nimi)
if len(kont) > 3:
print (nimi, len(kont), kont)
# for k in kont:
# if validate_mobile(k):
# print(" > " + k)
# elif validate_email(k):
# print(" = " + k)
# else:
# print(" & " + k.strip())
res.append(mob)
res.append(email)
res.append(addr)
#print(res)
with open('ep2019-kandidaadid.csv', 'a') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(res)
csv_file.close()