-
Notifications
You must be signed in to change notification settings - Fork 2
/
v1.scraper.py
executable file
·160 lines (128 loc) · 5.28 KB
/
v1.scraper.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
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
#!/usr/bin/env python
import sys
import signal
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import StaleElementReferenceException
def sigint(signal, frame):
sys.exit(0)
class Scraper(object):
def __init__(self):
self.url = 'http://icds-wcd.nic.in/icds/icdsawc.aspx'
self.driver = webdriver.PhantomJS()
self.driver.set_window_size(1120, 550)
#--- STATE -----------------------------------------------------
def get_state_select(self):
path = '//select[@id="ctl00_ContentPlaceHolder1_dropstate"]'
state_select_elem = self.driver.find_element_by_xpath(path)
state_select = Select(state_select_elem)
return state_select
def select_state_option(self, value, dowait=True):
'''
Select state value from dropdown. Wait until district dropdown
has loaded before returning.
'''
path = '//select[@id="ctl00_ContentPlaceHolder1_dropdistrict"]'
district_select_elem = self.driver.find_element_by_xpath(path)
def district_select_updated(driver):
try:
district_select_elem.text
except StaleElementReferenceException:
return True
except:
pass
return False
state_select = self.get_state_select()
state_select.select_by_value(value)
if dowait:
wait = WebDriverWait(self.driver, 10)
wait.until(district_select_updated)
return self.get_state_select()
#--- DISTRICT --------------------------------------------------
def get_district_select(self):
path = '//select[@id="ctl00_ContentPlaceHolder1_dropdistrict"]'
district_select_elem = self.driver.find_element_by_xpath(path)
district_select = Select(district_select_elem)
return district_select
def select_district_option(self, value, dowait=True):
'''
Select district value from dropdown. Wait until district dropdown
has loaded before returning.
'''
path = '//select[@id="ctl00_ContentPlaceHolder1_dropdistrict"]'
district_select_elem = self.driver.find_element_by_xpath(path)
def district_select_updated(driver):
try:
district_select_elem.text
except StaleElementReferenceException:
return True
except:
pass
return False
district_select = self.get_district_select()
district_select.select_by_value(value)
if dowait:
wait = WebDriverWait(self.driver, 10)
wait.until(district_select_updated)
return self.get_district_select()
#--- PROJECT ---------------------------------------------------
def get_project_select(self):
path = '//select[@id="ctl00_ContentPlaceHolder1_dropproject"]'
project_select_elem = self.driver.find_element_by_xpath(path)
project_select = Select(project_select_elem)
return project_select
def select_project_option(self, value, dowait=True):
project_select = self.get_project_select()
project_select.select_by_value(value)
return self.get_project_select()
def load_page(self):
self.driver.get(self.url)
def page_loaded(driver):
path = '//select[@id="ctl00_ContentPlaceHolder1_dropstate"]'
return driver.find_element_by_xpath(path)
wait = WebDriverWait(self.driver, 10)
wait.until(page_loaded)
def scrape(self):
def states():
state_select = self.get_state_select()
state_select_option_values = [
'%s' % o.get_attribute('value')
for o
in state_select.options[1:]
]
for v in state_select_option_values:
state_select = self.select_state_option(v)
yield state_select.first_selected_option.text
def districts():
district_select = self.get_district_select()
district_select_option_values = [
'%s' % o.get_attribute('value')
for o
in district_select.options
if o.text != '-Select-'
]
for v in district_select_option_values:
district_select = self.select_district_option(v)
yield district_select.first_selected_option.text
def projects():
project_select = self.get_project_select()
project_select_option_values = [
'%s' % o.get_attribute('value')
for o
in project_select.options[1:]
]
for v in project_select_option_values:
project_select = self.select_project_option(v)
yield project_select.first_selected_option.text
self.load_page()
for state in states():
print state
for district in districts():
print 2*' ', district
for project in projects():
print 4*' ', project
if __name__ == '__main__':
signal.signal(signal.SIGINT, sigint)
scraper = Scraper()
scraper.scrape()