2013-10-05 125 views
2

我想提取一個給定的HTML這是這種形式的城市和國家的XPath提取HTML標籤

<table class="wikitable sortable"> 
<tr> 
<th>Name of City/Town</th> 
<th>Name of State</th> 
<th>Classification</th> 
<th>Population (2001)</th> 
<th>Population (2011)</th> 
</tr> 
<tr> 
<td><a href="/wiki/Abhayapuri" title="Abhayapuri">**Abhayapuri**</a></td> 
<td><a href="/wiki/Assam" title="Assam">**Assam**</a></td> 
<td>TC</td> 
<td style="text-align:right;">14,673</td> 
<td style="text-align:right;"></td> 
</tr> 

我試着這樣做 $x('//table/tbody/tr/td/a')

但其返回我不想要的結果(即列表包含ChileNodes,children,classList,innerHTML和其他元數據)。不知道我在做什麼錯

回答

3

這個XPath:

$x('//table/tbody/tr/td/a/text()') 

將讓你的城市和國家:

["**Abhayapuri**", "**Assam**"] 

此XPath會得到你的城市:

$x('//table/tbody/tr/td[1]/a/text()') 

["**Abhayapuri**"] 

而這個XPath會爲你指定狀態:

$x('//table/tbody/tr/td[2]/a/text()') 

["**Assam**"]