2015-09-19 68 views
0

我正在使用Jsoup爲數據刮取頁面,但數據不在特定標記中。將XPath轉換爲CSS選擇器

<strong>LABEL IS HERE</strong> DATA IS HERE

使用XPath我能夠得到的路徑//*[@id="center-text"]/text()[1]但不幸的是鍍鉻不允許我複製的CSS路徑。

我可以得到<strong> LABEL IS HERE</strong>的CSS路徑,但不能用於其他文本。有沒有辦法使用CSS Selector語言獲取這些數據?


的樣本數據

<div id="center-text"> 
     <strong>ifno</strong> data&nbsp;&nbsp;&nbsp; 
     <strong>ifno</strong> data&nbsp;&nbsp;&nbsp; 
     <strong>Tifno</strong> data 
     <br> 
     <strong>ifno</strong> data&nbsp;&nbsp;&nbsp; 
     <strong>ifno</strong> data&nbsp;&nbsp;&nbsp; 
     <strong>ifno</strong> data 
</div> 
+0

這條線是否從另一個標籤包裝? –

+0

@VoodooCoder看看我的編輯是數據來自哪裏的div。 – Mozzie

回答

2

在JSOUP你可以使用nextSibling方法:

公共節點nextSibling()

獲取此節點的下一個兄弟。

返回: 下一個兄弟,或空,如果這是最後一個兄弟

你應該出去:

Elements elements = doc.select("div[id=\"center-text\"] strong"); 

for(Element element : elements) { 
    System.out.println("nextSibling: " + element.nextSibling()); 
} 

結果將是:

nextSibling: data&nbsp;&nbsp;&nbsp; 
nextSibling: data&nbsp;&nbsp;&nbsp; 
nextSibling: data 
nextSibling: data&nbsp;&nbsp;&nbsp; 
nextSibling: data&nbsp;&nbsp;&nbsp; 
nextSibling: data 
+0

這個答案是正確的,但我確實必須將XPath更改爲css。它應該是'#center-text'。但否則它的作品。非常感謝你。 – Mozzie

+0

這個問題的基本問題是,你不能選擇一個文本節點與CSS選擇器:http://stackoverflow.com/questions/5688712/is-there-a-css-selector-for-text-nodes – luksch