1

米国議会図書館/トーマスのウェブサイトをスクレイピングしようとしています。この Python スクリプトは、サイトから 40 の請求書のサンプルにアクセスすることを目的としています (URL の # 1-40 識別子)。各法律の本文を解析し、本文/コンテンツを検索し、潜在的な複数のバージョンへのリンクを抽出してフォローしたいと考えています。

バージョンページにアクセスしたら、各法律の本文を解析し、本文/コンテンツを検索し、潜在的なセクションへのリンクを抽出してフォローします。

セクションページにアクセスしたら、請求書の各セクションの本文を解析したいと思います。

コードの Rules/LinkExtractor セグメントに問題があると思います。Python コードが実行され、開始 URL がクロールされますが、解析や後続のタスクは実行されません。

3 つの問題:

  1. 一部の請求書には複数のバージョンがありません (したがって、URL の本文部分にリンクがありません)
  2. 一部の請求書は短いためにリンクされたセクションがありませんが、いくつかの請求書はセクションへのリンクだけです。
  3. 一部のセクション リンクには、セクション固有のコンテンツだけが含まれているわけではありません。また、ほとんどのコンテンツは、前または後のセクション コンテンツが重複して含まれているだけです。

繰り返しますが、Scrapy がクロールまたは解析しないのはなぜですか?

from scrapy.item import Item, Field
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector

class BillItem(Item):
    title = Field()
    body = Field()

class VersionItem(Item):
    title = Field()
    body = Field()

class SectionItem(Item):
    body = Field()

class Lrn2CrawlSpider(CrawlSpider):
    name = "lrn2crawl"
    allowed_domains = ["thomas.loc.gov"]
    start_urls = ["http://thomas.loc.gov/cgi-bin/query/z?c107:H.R.%s:" % bill for bill in xrange(000001,00040,00001) ### Sample of 40 bills; Total range of bills is 1-5767

    ]

rules = (
        # Extract links matching /query/ fragment (restricting tho those inside the content body of the url)
        # and follow links from them (since no callback means follow=True by default).
        # Desired result: scrape all bill text & in the event that there are multiple versions, follow them & parse.
        Rule(SgmlLinkExtractor(allow=(r'/query/'), restrict_xpaths=('//div[@id="content"]')), callback='parse_bills', follow=True),

        # Extract links in the body of a bill-version & follow them.
       #Desired result: scrape all version text & in the event that there are multiple sections, follow them & parse.
        Rule(SgmlLinkExtractor(restrict_xpaths=('//div/a[2]')), callback='parse_versions', follow=True)
    )

def parse_bills(self, response):
    hxs = HtmlXPathSelector(response)
    bills = hxs.select('//div[@id="content"]')
    scraped_bills = []
    for bill in bills:
        scraped_bill = BillItem() ### Bill object defined previously
        scraped_bill['title'] = bill.select('p/text()').extract()
        scraped_bill['body'] = response.body
        scraped_bills.append(scraped_bill)
    return scraped_bills

def parse_versions(self, response):
    hxs = HtmlXPathSelector(response)
    versions = hxs.select('//div[@id="content"]')
    scraped_versions = []
    for version in versions:
        scraped_version = VersionItem() ### Version object defined previously
        scraped_version['title'] = version.select('center/b/text()').extract()
        scraped_version['body'] = response.body
        scraped_versions.append(scraped_version)
    return scraped_versions

def parse_sections(self, response):
    hxs = HtmlXPathSelector(response)
    sections = hxs.select('//div[@id="content"]')
    scraped_sections = []
    for section in sections:
        scraped_section = SectionItem() ## Segment object defined previously
        scraped_section['body'] = response.body
        scraped_sections.append(scraped_section)
    return scraped_sections

spider = Lrn2CrawlSpider()
4

2 に答える 2