【发布时间】:2019-06-03 04:52:10
【问题描述】:
设置
我正在尝试在 Wikipedia 上抓取法国地区的信息框。
具体来说,我需要获取每个地区的人口。对于每个地区,其人口在每个 wiki 页面的信息框中都有说明,例如见https://en.wikipedia.org/wiki/Mayotte。
HTML
对于示例页面,我感兴趣的信息框html部分如下所示,
<tr class="mergedtoprow">
<th colspan="2" style="text-align:center;text-align:left">Area
<div style="font-weight:normal;display:inline;"></div></th></tr>
<tr class="mergedrow">
<th scope="row"> • Total</th>
<td>374 km<sup>2</sup> (144 sq mi)</td></tr>
<tr class="mergedtoprow">
<th colspan="2" style="text-align:center;text- align:left">
Population
<div style="font-weight:normal;display:inline;">
(2017)
<sup id="cite_ref-census_1-0" class="reference">
<a href="#cite_note-census-1">[1]</a>
</sup>
</div>
</th>
</tr>
<tr class="mergedrow">
<th scope="row"> • Total</th>
<td>256,518</td>
</tr>
我需要得到人口数 256,518。
代码
我的计划是选择包含'Population' 字符串的tr,然后告诉selenium 选择它后面的tr。
以下代码成功选择了包含'Population'字符串的tr,
info_box = browser.find_elements_by_css_selector('.infobox').find_element_by_xpath('tbody')
for row in info_box.find_elements_by_xpath('./tr'):
if 'Population' in row.text:
print(row)
现在!如何告诉 Selenium 在选择 tr 之后选择 tr?
【问题讨论】:
标签: python html selenium html-table