LoginSignup
3
2

More than 5 years have passed since last update.

Scrapyでノーベル章受賞者の情報を取得する(PJDV 6.4章) - XPath

Last updated at Posted at 2018-04-02

概要

XPath基本事項

その他、参考にした記事。XML向け(タグが自由に定義できる)なことに注意。

最初のScrapyスパイダー(nwinners_list_spider.py)

コード

nwinners_list_spider.py

書籍に誤植があり、githubで修正されているようです。

誤_nwinners_list_spider.py(PJDV_p145)
            country = h2.xpath('span[@class="mw-headline"]'\
            'text()').extract()
正_nwinners_list_spider.py(github)
            country = h2.xpath('span[@class="mw-headline"]/text()')\
            .extract()

XPath記述箇所

h2s = response.xpath('//h2')
country = h2.xpath('span[@class="mw-headline"]/text()').extract()
winners = h2.xpath('following-sibling::ol[1]')
for w in winners.xpath('li'):
text = w.xpath('descendant-or-self::text()').extract()

参考

Scrapy

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2