5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

#概要

#準備
##webページの読み込み
以下のいずれかで読み込んでおきます。

view(response) してブラウザを起動させておくと便利です。

fetch.PNG

#XPath記述箇所
nwinners_list_spider.py

##h2s = response.xpath('//h2')

###説明

省略構文 完全な構文 意味 参考
//h2 /descendant-or-self::node()/child::h2 全h2要素 参考1
参考2

###調査

何が受け取れているか確認してみます。

h2.PNG

h2b1.png

h2b.png

##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()

#tips
##scrapy crawlからscrapy shellを起動する
https://doc.scrapy.org/en/latest/topics/shell.html#invoking-the-shell-from-spiders-to-inspect-responses

nwinners_shell.py
# -*- coding: utf-8 -*-
import scrapy
class NWinnerShell(scrapy.Spider):
    name = 'nwinner_shell'
    allowed_domains = ['en.wikipedia.org']
    start_urls = [
        "http://en.wikipedia.org/wiki/List_of_Nobel_laureates_by_country"
    ]
    def parse(self, response):
        scrapy.shell.inspect_response(response, self)

#参考
Scrapyの取得結果をブラウザで開く - Qiita
scrapyでよく使うxpath, cssのセレクタ-Python Snippets
Python Tips:リストから重複した要素を削除したい - Life with Python

5
4
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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?