scrapyで食べログないのデータを取得しようとして以下のようにItemとspiderを作成し、
scrapy crawl tabelog -o retaurants.jl
をコマンド実行しました。一応、retaurants.jlファイルは作成されるのですが、抽出内容が記述されずに、ファイルのみ作成されます。
何か問題があるのでしょうか?ご教授お願いします。
Item(item.py)
import json import scrapy class Restaurant(scrapy.Item): """ 食べログのレストラン情報 """ name = scrapy.Field() # spaiderのresponse.css()で取得したデータを保存するItemを作成 address = scrapy.Field() latitude= scrapy.Field() logitude = scrapy.Field() station = scrapy.Field() score = scrapy.Field()
spider(spiders/tabelog.py)
python
1from scrapy.spiders import CrawlSpider, Rule 2# CrawlSpider:定期的なウェブサイトをクロールするために最も一般的に使用されるスパイダー. 3# 一連のルールを定義してリンクをたどるための便利なメカニズムを提供. 4# CrawlSpiderの例はhttp://scrapy-ja.readthedocs.io/ja/latest/topics/spiders.html 5import re 6from scrapy.linkextractors import LinkExtractor 7from myproject.items import Restaurant 8 9class TabelogSpider(CrawlSpider): 10 name = "tabelog" # スパイダーの名前(ドメイン名)と同じの名前を付ける 11 allowed_domains = ["tabelog.com"] 12 start_urls =('http://tabelog.com/tokyo/rstLst/lunch/?LstCosT=2&RdoCosTp=1',) 13 # ↑クロールを開始するURLのリスト.ダウンロードされる最初のページはここにリストされたページになる 14 15 rules = [ # トピックへのルールをたどり、レスポンスをcallback関数で処理する。 16 # allowで正規表現にマッチしたURL「一覧ページ(P272)」の1~9ページまで抽出。。\dを\d+に変えると10ページ目以降も辿れる。 17 Rule(LinkExtractor(allow=r'/\w+/rstLst/lunch/\d'), follow=True), 18 19 # レストランの「詳細ページ(P272)」をたどり、parse_retaurant()でパース(処理)する] 20 Rule(LinkExtractor(allow=r'/\w+/A\d+/A\d+/\d+/$'), callback='parse_restaurant'),] 21 # リンクをたどるためのルールリスト。各Ruleは, サイトをクロールするための特定の動作を定義 22 23 def parse_restaurant(self, response): 24 """ 25 レストランの詳細ページをパースする 26 """ 27 # Google Static Mapの画像のURLから緯度と軽度を取得 28 latitude, longitude = response.css('img.js-map-lazyload::attr("data-original")').re( 29 r'makers=.*?%7C([\d.]+),([\d.]+)') 30 31 # キー(Itemで作成したscrapy.field())の値を指定して、ItemのclassのRestaurantオブジェクトを作成 32 item = Resraurant( 33 name=renponse.css('.display-name').xpath('string()').extract_first().strip(), 34 adress=renponse.css('[rel="v:address"]').xpath('string()').extract_first().strip(), 35 latitude=latitude, 36 longitude=longitude, 37 station=renponse.css('dt:contains("最寄り駅")+dd span::text').extract_first(), 38 score=renponse.css('[property="v:average"]::text').extract_first(),) 39 40 yield item 41
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/08 20:29
2018/04/08 22:18
2018/04/10 13:56