-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmand.py
executable file
·97 lines (85 loc) · 2.56 KB
/
mand.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
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import random
import requests
import bs4
import string
import csv
import time
import re
min = 101
max = 101+1099
amount = 1099
random.seed(123)
#data = random.sample(range(min, max), amount)
data = range(min, max)
url = "http://rk2015.vvk.ee/acquired-mandates.html"
#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("table", {"class", "mandates"})
#print(dist)
# ringkond = dist[0].text.split(" ")[-1]
# candname = soup.find_all("span", {"class", "uppercase"})
# nimi = candname[0].text.title()
heads = []
table = dist[0].find('thead')
print(table)
hhh = table.find_all("th")
for h in hhh:
heads.append(h.text)
print(heads)
#exit(1)
j=0
for di in dist:
j+=1
table = di.find(name='tbody')
rrr = table.find_all("tr")
for r in rrr:
if r.text.find("Nimekiri kokku") < 0:
#print(r)
td = 0
res = []
ddd = r.find_all("td")
for d in ddd:
if heads[td]=="Jrk nr":
print(heads[td], d.text)
elif heads[td]=="Reg nr":
print(heads[td], d.text)
res.append(d.text)
elif heads[td]=="Ringkond":
print(heads[td], d.text.title())
res.append(d.text.title())
elif heads[td]=="Kandidaat":
print(heads[td], d.text.title())
res.append(d.text.title())
elif heads[td]=="Hääli kokku":
print(heads[td], d.text.strip())
res.append(d.text.strip())
elif heads[td]=="Hääled välisriigist":
print(heads[td], d.text)
res.append(d.text.strip())
elif heads[td]=="E-hääled*":
print(heads[td], d.text)
res.append(d.text.strip())
else:
print(heads[td], d.text)
td += 1
if j==1:
res.append("Isikumandaat")
elif j==2:
res.append("Ringkonnamandaat")
elif j==3:
res.append("Kompensatsioonimandaat")
with open('rk2015-mandaadid.csv', 'a') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(res)
csv_file.close()