2017-02-27 114 views
0

我花了很多時間試圖用scrapy取消信息而沒有成功。 我的目標是衝浪通過類別和每個項目廢料標題,價格和標題的href鏈接。Scrapy無法取消物品,xpath無法正常工作

該問題似乎來自parse_items函數。我已經請與firepath XPath和我能夠選擇的想要的物品,所以也許我只是不抓怎麼樣的XPath被scrapy處理...

這裏是我的代碼

from scrapy.contrib.spiders import CrawlSpider, Rule 
from scrapy.linkextractors import LinkExtractor 
from scrapy.selector import Selector 
from ..items import electronic_Item 


class robot_makerSpider(CrawlSpider): 
    name = "robot_makerSpider" 
    allowed_domains = ["robot-maker.com"] 
    start_urls = [ 
     "http://www.robot-maker.com/shop/", 
    ] 

    rules = (

     Rule(LinkExtractor(
      allow=(
       "http://www.robot-maker.com/shop/12-kits-robots", 
       "http://www.robot-maker.com/shop/36-kits-debutants-arduino", 
       "http://www.robot-maker.com/shop/13-cartes-programmables", 
       "http://www.robot-maker.com/shop/14-shields", 
       "http://www.robot-maker.com/shop/15-capteurs", 
       "http://www.robot-maker.com/shop/16-moteurs-et-actionneurs", 
       "http://www.robot-maker.com/shop/17-drivers-d-actionneurs", 
       "http://www.robot-maker.com/shop/18-composants", 
       "http://www.robot-maker.com/shop/20-alimentation", 
       "http://www.robot-maker.com/shop/21-impression-3d", 
       "http://www.robot-maker.com/shop/27-outillage", 
       ), 
      ), 
      callback='parse_items', 
     ), 
    ) 


    def parse_items(self, response): 
     hxs = Selector(response) 
     products = hxs.xpath("//div[@id='center_column']/ul/li") 
     items = [] 

     for product in products: 
      item = electronic_Item() 
      item['title'] = product.xpath(
       "li[1]/div/div/div[2]/h2/a/text()").extract() 
      item['price'] = product.xpath(
       "div/div/div[3]/div/div[1]/span[1]/text()").extract() 
      item['url'] = product.xpath(
       "li[1]/div/div/div[2]/h2/a/@href").extract() 

      #check that all field exist 
      if item['title'] and item['price'] and item['url']: 
       items.append(item) 
     return items 

感謝您的幫助

回答

0

您的蜘蛛xpaths確實有問題。

您的產品的第一個xpath確實有效,但它不夠明確,可能很容易失敗。雖然產品詳細信息xpaths根本不起作用。

我知道了有工作:

products = response.xpath("//div[@class='product-container']") 
items = [] 

for product in products: 
    item = dict() 
    item['title'] = product.xpath('.//h2/a/text()').extract_first('').strip() 
    item['url'] = product.xpath('.//h2/a/@href').extract_first() 
    item['price'] = product.xpath(".//span[contains(@class,'product-price')]/text()").extract_first('').strip() 

所有現代的網站都非常友好解析HTML源(因爲他們需要解析它自己爲自己看中的CSS樣式和JavaScript函數)。

所以一般來說,你應該看看你想使用瀏覽器檢查工具(右鍵單擊 - >檢查元素)提取的節點的類和id名稱,而不是使用一些自動選擇工具。它更可靠,一旦掌握了它就不會花費太多的工作。

+0

謝謝你!我會從這裏小心翼翼。您能否向我解釋直接從響應中查找xpath而不是使用Selector(response)方法的影響? –

+0

@ArtFilPortraitArtistetisseu它本質上是一回事。 Response對象使用自己創建'Selector',所以你可以有一個方便的'response.selector'快捷方式,而不必每次都創建Selector。 'response.xpath'是'response.selector.xpath'的快捷方式。 [響應來源](https://github.com/scrapy/scrapy/blob/master/scrapy/http/response/text.py#L112)非常簡單,你可以自己給它一個高峯:) – Granitosaurus