2016-07-02 266 views
0

我完全難住這個bug。我試圖從此頁面拉出所有菜單項https://www.alloresto.fr/restaurant-livraison-a-domicile/restaurant/pizza-mia/angers-centre-ville/particuliers/carte。當它到達最內層for循環時,它會經歷一次迭代,然後繼續前進。這是非常意外的,我不知道是什麼原因造成的。以下是專用於解析此頁面的功能。For循環不迭代

def get_menu(self, response): 
    image_url = response.urljoin(response.xpath('//span/img/@src').extract_first()) 
    for menu_section in response.xpath("//div[@id = 'contenu_choixplats']/div"): 
     menu_section_name = menu_section.xpath('dl/dt/text()').extract_first() 
     for menu_item in menu_section.xpath('ul/li'): 
      item = Restaurant() 
      item['restaurant_url'] = response.url 
      item['restaurant_name'] = response.request.meta['restaurant_name'] 
      item['street_name'] = response.request.meta['street_name'] 
      item['street_number'] = response.request.meta['street_number'] 
      item['city'] = response.request.meta['city'] 
      item['zip_code'] = response.request.meta['zip_code'] 
      item['food_type'] = response.request.meta['food_type'] 
      item['image_urls'] = [image_url] 
      item['menu_category'] = menu_section_name 
      item['menu_item_title'] = menu_item.xpath('div/h3/text()').extract() 
      item['menu_item_details'] = menu_item.xpath('div/p/text()').extract_first() 
      item['menu_item_price'] = menu_item.xpath('div').css('div.product-price-with-offer').xpath('p/text()').extract_first() 
      yield item 

你看到我失蹤的東西嗎?感謝您的時間。

--UPDATE ---

這是完整的代碼。如果問題出在get_menu函數之外,我提供它。請注意,get_menu函數只能在從網站索引深入兩頁後才能找到。 蜘蛛/ alloresto_spider.py 進口scrapy 進口重新 從french_scraping.items進口餐廳

class DmozSpider(scrapy.Spider): 
    name = "alloresto" 
    allowed_domains = ['alloresto.fr'] 
    start_urls = ["https://www.alloresto.fr/livraison/villes/"] 

    def parse(self, response): 
     for sel in response.xpath('//ul/li/a/@href'): 
      url = response.urljoin(sel.extract()) 
      yield scrapy.Request(url, callback=self.restaurants_for_this_city) 

    def restaurants_for_this_city(self, response): 
     for restaurant in response.xpath('//article/div'): 
      restaurant_url = response.urljoin(restaurant.xpath('a/@href').extract_first()) 
      restaurant_name = restaurant.xpath('div/section[@class="restaurantDetails"]/h3/a/text()').extract_first() 
      full_address = restaurant.xpath('div/section[@class="restaurantDetails"]/address/text()').extract_first() 
      extracts = re.search(r'^([\d-]*?)\W(.*?),\W(.*?)\W(\d\d\d\d\d)', full_address) 
      try: 
       street_number = extracts.group(1) 
      except: 
       continue 
      street_name = extracts.group(2) 
      city = extracts.group(3) 
      zip_code = extracts.group(4) 
      food_type = restaurant.xpath('div/section/p').css('.restaurantCuisines').xpath('text()').extract() 
      meta_data = { 
       'restaurant_url': restaurant_url, 
       'restaurant_name': restaurant_name, 
       'street_number': street_number, 
       'street_name': street_name, 
       'city': city, 
       'zip_code': zip_code, 
       'food_type': food_type} 
      yield scrapy.Request(restaurant_url, meta=meta_data, callback=self.get_menu) 
     # get info on next page 
     next_page = response.css('.next').xpath('a/@href').extract() 
     if len(next_page) > 0: 
      url = response.urljoin(next_page[0]) 
      yield scrapy.Request(url, callback=self.restaurants_for_this_city) 

    def get_menu(self, response): 
     image_url = response.urljoin(response.xpath('//span/img/@src').extract_first()) 
     for menu_section in response.xpath("//div[@id = 'contenu_choixplats']/div"): 
      menu_section_name = menu_section.xpath('dl/dt/text()').extract_first() 
      for menu_item in menu_section.xpath('ul/li'): 
       item = Restaurant() 
       item['restaurant_url'] = response.url 
       item['restaurant_name'] = response.request.meta['restaurant_name'] 
       item['street_name'] = response.request.meta['street_name'] 
       item['street_number'] = response.request.meta['street_number'] 
       item['city'] = response.request.meta['city'] 
       item['zip_code'] = response.request.meta['zip_code'] 
       item['food_type'] = response.request.meta['food_type'] 
       item['image_urls'] = [image_url] 
       item['menu_category'] = menu_section_name 
       item['menu_item_title'] = menu_item.xpath('div/h3/text()').extract() 
       item['menu_item_details'] = menu_item.xpath('div/p/text()').extract_first() 
       item['menu_item_price'] = menu_item.xpath('div').css('div.product-price-with-offer').xpath('p/text()').extract_first() 
       yield item 

items.py

import scrapy 

class Restaurant(scrapy.Item): 
    # define the fields for your item here like: 
    # name = scrapy.Field() 
    restaurant_url = scrapy.Field() 
    street_number = scrapy.Field() 
    restaurant_name = scrapy.Field() 
    street_name = scrapy.Field() 
    city = scrapy.Field() 
    zip_code = scrapy.Field() 
    food_type = scrapy.Field() 
    menu_category = scrapy.Field() 
    menu_item_title = scrapy.Field() 
    menu_item_details = scrapy.Field() 
    menu_item_price = scrapy.Field() 
    image_urls = scrapy.Field() 
    images = scrapy.Field() 
    pass 

settings.py

BOT_NAME = 'french_scraping' 
SPIDER_MODULES = ['french_scraping.spiders'] 
NEWSPIDER_MODULE = 'french_scraping.spiders' 
ROBOTSTXT_OBEY = True 
ITEM_PIPELINES = {'scrapy.pipelines.images.ImagesPipeline': 1} 
IMAGES_STORE = '/Users/drew/Desktop/frenchscraping/french_scraping' 

pipelines.py

class FrenchScrapingPipeline(object): 
    def process_item(self, item, spider): 
     return item 
+0

它看起來像它返回一個生成器:[關鍵字yield在Python中做什麼?](http://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do-in-python ) – nlloyd

+1

所以我們已經有了這個功能,但它是如何被調用的?請提供[mcve]。我懷疑電話中存在問題。 –

+0

我做了一個編輯。請參閱上面的完整代碼。 – DrewSSP

回答

0

get_menu(self,response)是一個生成器,因爲它使用yield語句。

當調用生成器函數時,它返回一個生成器對象,而不開始執行該函數。爲了讓代碼運行,您需要在'for'循環中使用get_menu(self,response),或者將它傳遞給迭代的任何函數或構造。