Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
54 additions
and
5 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
|
||
import itertools as it | ||
import sqlite3 | ||
|
||
from scrape_elected import decap_name, start_session, title_match | ||
|
||
|
||
def tidy_up_row(row): | ||
area, first, last, party, *_ = (i.strip() for i in row) | ||
return (decap_name(title_match.sub('', first)), decap_name(last), | ||
party, '2013', area) | ||
|
||
|
||
def parse_table(doc): | ||
return (tidy_up_row(i.text_content() for i in v.xpath('./td')) | ||
for v in doc.xpath('//table[@id="ctl00_ContentPlaceHolder1_ASPxPageControl1_ASPxGridView3_DXMainTable"]' | ||
'//tr[@class="dxgvDataRow"]')) | ||
|
||
|
||
def parse_pages(session): | ||
while True: | ||
yield parse_table(session.document()) | ||
page = session.at_xpath('//table[@id="ctl00_ContentPlaceHolder1_ASPxPageControl1_ASPxGridView3_DXPagerBottom"]' | ||
'//td[@class="dxpPageNumber dxpCurrentPageNumber"]' | ||
'/following-sibling::td[@class="dxpPageNumber"]') | ||
if not page: | ||
break | ||
page.click() | ||
while session.at_css('#ctl00_ContentPlaceHolder1_ASPxGridView1_LPV'): | ||
# Wait for the table to be updated | ||
... | ||
|
||
|
||
def main(): | ||
session = start_session('Milletvekillerimiz1.aspx') | ||
with sqlite3.connect('data.sqlite') as c: | ||
c.execute('''\ | ||
CREATE TABLE IF NOT EXISTS data | ||
(first_name, last_name, party, term, area, | ||
UNIQUE (first_name, last_name, party, term, area))''') | ||
c.executemany('INSERT OR REPLACE INTO data VALUES (?, ?, ?, ?, ?)', | ||
it.chain.from_iterable(parse_pages(session))) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters