-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelection_data.py
More file actions
54 lines (47 loc) · 1.05 KB
/
election_data.py
File metadata and controls
54 lines (47 loc) · 1.05 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
import json
import requests
import urllib2
import bs4
#api_key = '6kfcvgrshs9sa8yfwxeeeg3h'
url = 'https://en.wikipedia.org/wiki/Elections_in_India'#'http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/top_rentals.json?apikey=' + api_key
data = urllib2.urlopen(url).read()
## create dictionary from JSON
#dataDict = json.loads(data)
soup = bs4.BeautifulSoup(data)
#print soup.prettify
#td=soup.findAll('td')
#print td
#tdsoup = bs4.BeautifulSoup(td)
l=[]
total_seats=[]
winner=[]
start =0
n=4
for i in soup.findAll('td'):
if start == 2:
winner.append(i)
start=0
if start == 1:
total_seats.append(i)
start=2
if i.find('a'):
try:
pos=i.find('a')
if pos.get('href') == '/wiki/'+str(n)+'th_Lok_Sabha':
print i
start =1
n=n+1
except:
print 1
# print i[pos:pos+20]
# print i.get('href')#== '/wiki/1st_Lok_Sabha':
# print i
#l.append(i)
#trin=total_seats[0]
print total_seats
print winner
for i in range(0,n-2):
print str(total_seats[i])[4:7]
print str(winner[i])[4:7]
## expore dictionary
#print dataDict.keys()