6.4 最初のScrapyスパイダー
#nminners_list_spider.py
import scrapy
import re
class NWinnerItem(scrapy.Item):
country = scrapy.Field()
name = scrapy.Field()
link_text = scrapy.Field()
class NWinnerSpider(scrapy.Spider):
name = 'nwinners_list'
allowed_domains = ['en.wikipedia.org']
start_urls = ["https://en.wikipedia.org/wiki/List_of_Nobel_laureates_by_country"]
def parse(self,response):
h2s = response.xpath('//h2')
for h2 in h2s:
country = h2.xpath('span[@class="mw-headline"]/text()').extract()
if country:
winners = h2.xpath('following-sibling::ol[1]')
for w in winners.xpath('li'):
text = w.xpath('descendant-or-self::text()').extract()
yield NWinnerItem(country=country[0],name=text[0],link_text=''.join(text))
これを実行するとnobel_winnersフォルダにnobel_winners.jsonが出力される。
よく見ると国が1段ずれている、最初の人の国はSummaryになっている。
#nminners_list_spider.py
import scrapy
import re
class NWinnerItem(scrapy.Item):
country = scrapy.Field()
name = scrapy.Field()
link_text = scrapy.Field()
class NWinnerSpider(scrapy.Spider):
name = 'nwinners_list'
allowed_domains = ['en.wikipedia.org']
start_urls = ["https://en.wikipedia.org/wiki/List_of_Nobel_laureates_by_country"]
def parse(self,response):
h2s = response.xpath('//h2')
for h2 in h2s[2:]: #indexを2つずらしてループを廻すようにした
country = h2.xpath('span[@class="mw-headline"]/text()').extract()
if country:
winners = h2.xpath('following-sibling::ol[1]')
for w in winners.xpath('li'):
wdata = process_winner_li(w,country[0])
正常にになった。個数を数えたら1068であった。本とは違う。減る筈はないとおもうが変更があったのでしょう。