我想從一些HTML中提取多個值,並且我認爲XPath可能是實現此目的的理想方法。使用XPath從HTML獲取多個值
我想這樣做的什麼是通過具有類data
則循環中的每個循環tr
得到我所需要的數據,是route_number
的a
內的文本(也在標題)和via
文本。
的HTML低於:
<tr class="data"><th class="route_number"><a href="/routes/west-midlands/B001v/?tab=" title="Dudley - Sedgley - Wolverhampton - Tettenhall Wood"><span class="route_number small_curvy">1</span></a></th>
<td class="main_and_via">
<a href="/routes/west-midlands/B001v/?tab=" title="Dudley - Sedgley - Wolverhampton - Tettenhall Wood">Dudley - Sedgley - Wolverhampton - Tettenhall Wood</a>
<span class="via"><strong>via</strong> Dudley Road and Tettenhall Road</span>
</td>
</tr><tr class="data"><th class="route_number"><a href="/routes/west-midlands/B002/?tab=" title="Birmingham City Centre - Sparkbrook - Yardley Wood - Warstock/Maypole"><span class="route_number small_curvy">2</span></a></th>
<td class="main_and_via">
<a href="/routes/west-midlands/B002/?tab=" title="Birmingham City Centre - Sparkbrook - Yardley Wood - Warstock/Maypole">Birmingham City Centre - Sparkbrook - Yardley Wood - Warstock/Maypole</a>
<span class="via"><strong>via</strong> Yardley Wood Road</span>
</td>
</tr>
通過每個tr
然後有單獨的查詢循環的route number
,anchor text
和via text
理想或可將其與一個單一的XPath查詢做些什麼呢?
不會真的不僅僅是有什麼不同使用getAttribute()和getElementsByClassName – runspired