1. sirex
  2. scrapy-vrk

Source

scrapy-vrk / src / vrk / spiders / interests_declaration_2012.py

# coding: utf-8

# This file is part of scrapy-vrk.
#
# scrapy-vrk is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# scrapy-vrk is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with scrapy-vrk. If not, see <http://www.gnu.org/licenses/>.

"""
set makeprg=bin/scrapy\ parse\ --spider=interests-declaration-2012\ --callback=parse_interests_declaration\ 'http://www.vrk.lt/rinkimai/416_en/Kandidatai/Kandidatas67880/Kandidato67880InteresuDeklaracija.html'

"""

from scrapy.contrib.loader import XPathItemLoader
from scrapy.contrib.spiders import CrawlSpider
from scrapy.contrib.spiders import Rule
from scrapy.selector import HtmlXPathSelector

from vrk.items import PersonActivity
from vrk.items import PersonAgreements
from vrk.items import PersonInterests
from vrk.linkextractors import LxmlLinkExtractor


class InterestsDeclaration_2012_Spider(CrawlSpider):
    name = 'interests-declaration-2012'
    allowed_domains = ('vrk.lt',)

    start_urls = (
        'http://www.vrk.lt/2012_seimo_rinkimai/output_lt/rezultatai_daugiamand_apygardose/rezultatai_daugiamand_apygardose1turas.html',
    )

    rules = (
        Rule(LxmlLinkExtractor(allow=[r'partijos_pirmumo_balsai\d+.html$']), follow=True),
        Rule(LxmlLinkExtractor(allow=[r'Kandidato\d+Anketa.html$']), follow=True),
        Rule(LxmlLinkExtractor(allow=[r'Kandidato\d+InteresuDeklaracija.html$']),
                               'parse_interests_declaration'),
    )

    def parse_interests_declaration(self, response):
        hxs = HtmlXPathSelector(response).select(
            '/html/body/div/div/div[2]/div[2]/div/div[2]/table/tr/td[2]/b'
        )

        person = hxs.select('text()').extract()[0]

        hxs = HtmlXPathSelector(response).select(
            '/html/body/div/div/div[2]/div[2]/div/div[2]/table[3]/tr[count(child::td)=8]'
        )

        for row in hxs:
            l = XPathItemLoader(PersonInterests(), row)
            l.add_value('person',                           person)
            l.add_xpath('person_indication',                'td[1]/text()')
            l.add_xpath('state',                            'td[2]/text()')
            l.add_xpath('relation_with_legal_person',       'td[3]/text()')
            l.add_xpath('start_date',                       'td[4]/text()')
            l.add_xpath('end_date',                         'td[5]/text()')
            l.add_xpath('entity_code',                      'td[6]/text()')
            l.add_xpath('title',                            'td[7]/text()')
            l.add_xpath('other',                            'td[8]/text()')

            yield l.load_item()


        hxs = HtmlXPathSelector(response).select(
            '/html/body/div/div/div[2]/div[2]/div/div[2]/table[4]/tr[count(child::td)=10]'
        )

        for row in hxs:
            l = XPathItemLoader(PersonAgreements(), row)
            l.add_value('person',                           person)
            l.add_xpath('person_indication',                'td[1]/text()')
            l.add_xpath('agreement',                        'td[2]/text()')
            l.add_xpath('agreement_from',                   'td[3]/text()')
            l.add_xpath('other_party_of_agreement',         'td[4]/text()')
            l.add_xpath('start_date',                       'td[5]/text()')
            l.add_xpath('end_date',                         'td[6]/text()')
            l.add_xpath('amount',                           'td[7]/text()')
            l.add_xpath('entity_code',                      'td[8]/text()')
            l.add_xpath('title',                            'td[9]/text()')
            l.add_xpath('description',                      'td[10]/text()')

            yield l.load_item()


        hxs = HtmlXPathSelector(response).select(
            '/html/body/div/div/div[2]/div[2]/div/div[2]/table[5]/tr[count(child::td)=5]'
        )

        for row in hxs:
            l = XPathItemLoader(PersonActivity(), row)
            l.add_value('person',                               person)
            l.add_xpath('person_indication', 'td[1]/text()')
            l.add_xpath('activity_type',                        'td[2]/text()')
            l.add_xpath('start_date',                           'td[3]/text()')
            l.add_xpath('end_date',                             'td[4]/text()')
            l.add_xpath('other',                                'td[5]/text()')

            yield l.load_item()