2016-08-08 43 views
0

我需要在下面的html中只提取19.10值,但是我的xpath不起作用。提前致謝。從Scrapy中的xpath選擇器中刪除跨度div

<div class="class1">19.10 
     <span class="class2"><br></span> 
</div> 

的Xpath:

//div[@class='class1'][not(preceding::span[@class='class2'])]/text() 

結果:

[u'19.10\n\t\t\t\t\t\t\t', u'\n\t\t\t\t\t\t'] 

回答

1

你想在這裏拿到第一個文本元素。這樣做的方法很少。使用XPath:

"/div[@class='class1'][not(preceding::span[@class='class2'])]/text()[1]" 

或用後處理:

# just first element 
response.xpath("xpath").extract_first() 

,或者如果你熟悉項目裝載機:

from scrapy.loader.processors import TakeFirst 
from scrapy.loader import ItemLoader 
class MyItemLoader(ItemLoader): 
    myfield_out = TakeFirst() 
ml = MyItemLoader() 
ml.add_xpath('myfield', 'xpath') 
+0

它的工作非常感謝。 – DevOps

1

嘗試下面的XPath: -

string(//div[@class='class1']) 

(//div[@class='class1']/text())[1] 
+0

它的工作非常感謝。 – DevOps