2017-03-09 49 views
0

我想凑在此链接给定网页单列相对多行数据 - http://new-york.eat24hours.com/picasso-pizza/19053如何提取scrapy

在这里,我试图让所有喜欢的地址和电话等可能的细节因此,我已经提取了名称,电话,地址,评论,评分。 但我也想在这里提取餐厅的完整菜单(价格与物品的名称)。 所以,我不知道如何管理这些数据到csv的输出中。

单个网址的其余数据将是单一的,但菜单中的项目总是数量不同。

这里下面是我的代码,因此远

import scrapy 
from urls import start_urls 

class eat24Spider(scrapy.Spider): 
    AUTOTHROTTLE_ENABLED = True 
    name = 'eat24' 

    def start_requests(self): 
     for x in start_urls: 
      yield scrapy.Request(x, self.parse) 

    def parse(self, response): 

     brickset = response 
     NAME_SELECTOR = 'normalize-space(.//h1[@id="restaurant_name"]/a/text())' 
     ADDRESS_SELECTION = 'normalize-space(.//span[@itemprop="streetAddress"]/text())' 
     LOCALITY = 'normalize-space(.//span[@itemprop="addressLocality"]/text())' 
     REGION = 'normalize-space(.//span[@itemprop="addressRegion"]/text())' 
     ZIP = 'normalize-space(.//span[@itemprop="postalCode"]/text())' 
     PHONE_SELECTOR = 'normalize-space(.//span[@itemprop="telephone"]/text())' 
     RATING = './/meta[@itemprop="ratingValue"]/@content' 
     NO_OF_REVIEWS = './/meta[@itemprop="reviewCount"]/@content' 
     OPENING_HOURS = './/div[@class="hours_info"]//nobr/text()' 
     EMAIL_SELECTOR = './/div[@class="company-info__block"]/div[@class="business-buttons"]/a[span]/@href[substring-after(.,"mailto:")]' 

     yield { 
      'name': brickset.xpath(NAME_SELECTOR).extract_first().encode('utf8'), 
      'pagelink': response.url, 
      'address' : str(brickset.xpath(ADDRESS_SELECTION).extract_first().encode('utf8')+', '+brickset.xpath(LOCALITY).extract_first().encode('utf8')+', '+brickset.xpath(REGION).extract_first().encode('utf8')+', '+brickset.xpath(ZIP).extract_first().encode('utf8')), 
      'phone' : str(brickset.xpath(PHONE_SELECTOR).extract_first()), 
      'reviews' : str(brickset.xpath(NO_OF_REVIEWS).extract_first()), 
      'rating' : str(brickset.xpath(RATING).extract_first()), 
      'opening_hours' : str(brickset.xpath(OPENING_HOURS).extract_first()) 
     } 

我很抱歉,如果我提出这个混乱,但任何形式的帮助将不胜感激。 提前谢谢!

回答

0

如果你想提取完整的餐厅菜单,首先,你需要找到元素谁同时包含名称和价格:

menu_items = response.xpath('//tr[@itemscope]') 

之后,你可以简单地进行循环和迭代餐厅项目附加名称和价格清单:

menu = [] 
for item in menu_items: 
    menu.append({ 
     'name': item.xpath('.//a[@class="cpa"]/text()').extract_first(), 
     'price': item.xpath('.//span[@itemprop="price"]/text()').extract_first() 
     }) 

最后,你可以添加新的“菜单”键,你的字典:

yield {'menu': menu} 

另外,我建议你使用scrapy项目用于存储数据刮掉: https://doc.scrapy.org/en/latest/topics/items.html

对于控制台输出的csv文件使用scrapy饲料出口数据类型:

scrapy crawl yourspidername -o restaurants.csv 
+0

谢谢你这么much..i会尝试并让你知道它是否工作或不。 –

+0

为了简单起见,我在menu_items中编辑了xpath。 – vold

+0

谢谢,真的帮助.. –