LoginSignup
2
2

More than 5 years have passed since last update.

【勉強ノート】ウェブサイトからデータ収集のための Scrapy Shell

Posted at

前回はScrapy基礎を勉強しました。
今回はScrapy Shellについて勉強します。

注意事項

この投稿は単純な勉強の記録です。私はWeb Scrapingに経験がないですから、間違ったことをやる可能性も高いと思います。その時はコメントで教えていただけると幸いです。そして、この投稿の内容はScrapyの公式ホームページを参考にしています。

今回の流れ

  1. Scrapy Shellとは
  2. Scrapy Shellを利用しデータ抽出

1. Scrapy Shell とは

Scrapy Shellは単純にScrapyの便利ツールとobjectが提供できているpython shellです。本当にそれだけですね。ipythonがインストールされている場合はデフォルトのpython shellの代わりにipython shellが利用されます。ipythonの方が便利なので私はipythonを利用します。

Scrapy Shellが便利なところは、spiderを実際に起動しなくてものウェブページscrapyingのテストが出来るところです。実際に使ってみましょう

2. Scrapy Shellを利用しデータ抽出

Scrapy Shellを起動するには下記のようにターミナルに入力します。

$ scrapy shell <url>

今回はQuatesサイトを対象にしますので、下記のようになります。

$ scrapy shell 'http://quotes.toscrape.com/page/1/'

成功すると、下記のような結果になるになります。

[ ... Scrapy log here ... ]
2018-07-07 11:41:54 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://quotes.toscrape.com/page/1/> (referer: None)
[s] Available Scrapy objects:
[s]   scrapy     scrapy module (contains scrapy.Request, scrapy.Selector, etc)
[s]   crawler    <scrapy.crawler.Crawler object at 0x7f94eb735630>
[s]   item       {}
[s]   request    <GET http://quotes.toscrape.com/page/1/>
[s]   response   <200 http://quotes.toscrape.com/page/1/>
[s]   settings   <scrapy.settings.Settings object at 0x7f94ea4bd748>
[s]   spider     <QuotesSpider 'quotes' at 0x7f94ea04c780>
[s] Useful shortcuts:
[s]   fetch(url[, redirect=True]) Fetch URL and update local objects (by default, redirects are followed)
[s]   fetch(req)                  Fetch a scrapy.Request and update local objects 
[s]   shelp()           Shell help (print this help)
[s]   view(response)    View response in a browser
In [1]: 

ご覧の通り、Scrapyの便利なobjectとshortcutsが使えることが明記されています。
このobjectとshortcutsを使ってtitleのデータを抽出してみます。

In [1]: response.xpath('//title')
Out[1]: [<Selector xpath='//title' data='<title>Quotes to Scrape</title>'>]

簡単にタイトルのデータを抽出出来ることが分かります。私はxpathを利用し抽出しますが、cssの方が楽な方はcssでも良いと思います。

Scrapy Shellの公式ホームページにもっと詳しい説明が書いてますので参考にしてください。

2
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
2
2