2017-03-27 72 views
1

當我把一個點,使XPath的相對我的,它返回{「名」:「無」},當我刪除了點,直到循環結束XPath的相對路徑返回無

.//*[@id="listingAds"]/section/section/ul/li[1]/a/section/h2 
重複相同的數據

下面是完整的代碼

import scrapy 
class BrickSetSpider(scrapy.Spider): 
    name = "brickset_spider" 
    start_urls = ['https://www.leboncoin.fr/annonces/offres/bretagne/'] 

    def parse(self, response): 
     SET_SELECTOR = '.dontSwitch .trackable' 
     for annonce in response.css(SET_SELECTOR): 


      NAME_SELECTOR = './/*[@id="listingAds"]/section/section/ul/li[1]/a/section/h2' 
      NAME_CATEGORIE = '.item_title+ .item_supp ::text' 
      NAME_PLACE = '.item_supp+ .item_supp ::text' 
      NAME_PRICE = '.item_price ::text' 

      yield { 
      'name': annonce.xpath(NAME_SELECTOR).extract_first(), 
      } 

回答

0

那是因爲你已經SET_SELECTOR讓你進入目錄的列表。換句話說,每個annonce選擇器對應於每個搜索結果元素(每個列表)。適當調整您的NAME_SELECTOR

NAME_SELECTOR = './/h2/text()'