-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspider.py
84 lines (68 loc) · 2.81 KB
/
spider.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
from bs4 import BeautifulSoup
import scrapy
from open_parliament.parsers import Row, PersonalPage, CommitteesPage
class NationalratsSpider(scrapy.Spider):
"""
A scraper for MPs of the Austrian Nationalrat_.
Scrapes biographical data, contact information, party information and committee membership.
.. seealso:: :mod:`open_parliament.parsers`
.. _Nationalrat: https://www.parlament.gv.at/WWER/NR/AKT/
"""
name = "nationalrat"
BASE = "https://www.parlament.gv.at"
start_urls = [BASE + "/WWER/NR/AKT/index.shtml"]
def parse(self, response):
soup = BeautifulSoup(response.text, features="html.parser")
next_page = soup.find("div", class_="paginationRechts").a.attrs["href"]
if next_page is not None:
yield response.follow(next_page, self.parse_table)
def parse_table(self, response):
soup = BeautifulSoup(response.text, features="html.parser")
table = soup.find(
"table",
summary="Liste zeigt die ausgewählten Abgeordnete, die derzeit ein Mandat innehaben",
)
rows = table.find_all("tr")
for row in rows:
parser = Row(row)
mp = parser.parse()
if mp:
request = scrapy.Request(self.BASE + mp["url"], callback=self.parse_mp)
request.meta["mp"] = mp
yield request
def parse_mp(self, response):
mp = response.meta["mp"]
soup = BeautifulSoup(response.text, features="html.parser")
is_president = soup.find(id="biogr_Einleitung") is not None
if is_president:
request = scrapy.Request(
self.BASE + mp["url"] + "zurPerson.shtml", callback=self.parse_president
)
request.meta["mp"] = mp
yield request
else:
mp = self.parse_details(response, False)
if mp["in_committees"]:
url = self.BASE + mp["url"] + "index.shtml#tab-Ausschuesse"
request = scrapy.Request(url, callback=self.parse_committees)
request.meta["mp"] = mp
yield request
else:
yield mp
def parse_president(self, response):
mp = self.parse_details(response, True)
url = self.BASE + mp["url"] + "ausschuesse.shtml"
request = scrapy.Request(url, callback=self.parse_committees)
request.meta["mp"] = mp
yield request
def parse_committees(self, response):
mp = response.meta["mp"]
committee_parser = CommitteesPage(response.text)
mp.update(committee_parser.parse())
yield mp
@staticmethod
def parse_details(response, is_president):
mp = response.meta["mp"]
parser = PersonalPage(response.text)
mp.update(parser.parse(is_president))
return mp