2014-03-12 125 views
0

我在scrapy中創建了一個項目,我從網頁中刮取(顯然!)特定數據。從選擇器中刪除子節點

items = sel.xpath('//div[@class="productTiles cf"]/ul').extract() 
    for item in items: 
      price = sel.xpath('//ul/li[@class="productPrice"]/span/span[@class="salePrice"]').extract() 
      print price 

這將產品以下結果:

u'<span class="salePrice">$20.43\xa0<span class="reducedFrom">$40.95</span></span>',  
u'<span class="salePrice">$20.93\xa0<span class="reducedFrom">$40.95</span></span> 

我需要得到公正是salePrice,例如20.43和20.93,而忽略其他標籤和其餘數據。任何幫助在這裏將不勝感激。

回答

0

貌似解決方案如下:

//ul/li[@class="productPrice"]/span/span[@class="salePrice"]//text() 

它會抓住我正在尋找正確的元素的只是文本,就像這樣:

u'$20.43\xa0', u'$20.93\xa0' 

現在就可以解析它,以消除最後的不必要的垃圾,我就定了。如果有人有更優雅的解決方案,我很樂意看到它。

0

span[@class="salePrice"]返回span與其子女。

這應該得到的只是頂部span全文:

sel.xpath('//ul/li[@class="productPrice"]/span/span[@class="salePrice"]/text()').extract()[0]