Anurag Sharma
Anurag Sharma

Reputation: 5049

xpath for extracting html tags

I want to extract cities and state from a given html which is in this form

<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>

I tried doing this $x('//table/tbody/tr/td/a')

but its returning me undesired result(i.e list containing ChileNodes, children, classList, innerHTML and other metadata). Dont know what I am doing wrong

Upvotes: 2

Views: 120

Answers (1)

kjhughes
kjhughes

Reputation: 111726

This XPath:

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

will get you the city and state:

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

This XPath will get you the city:

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

["**Abhayapuri**"]

And this XPath will get you the state:

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

["**Assam**"]

Upvotes: 3

Related Questions