table = plan1.find('table', id = 'planComparison')
pcp = table.findChildren(text=' Doctor Visit - Primary Care ')
我使用上面的代码将 pcp 变量设置为“Doctor Visit - Primary Care”单元格。我需要来自旁边单元格的信息(具体情况会有所不同)。
如何拉动相邻的单元格?有没有办法返回 pcp 变量单元格的行号?欢迎任何意见。
最佳答案
获取下一个td
,使用nextSibling
功能。这可能有点棘手,因为空格可以被视为下一个兄弟,所以你必须尝试一些事情。我将您的代码修改为:
table = plan1.find('table', id = 'planComparison')
pcp = table.find('td',text=' Doctor Visit - Primary Care ')
print(pcp.nextSibling.nextSibling.text)
输出为:
Copay: No Charge after deductible; Coinsurance: No Charge after deductible
注意我必须打电话nextSibling
两次到达 td
标记你想要的,然后调用text
摆脱<td>
标签。
另请注意,我指定您想要 td
在 find
而不是一般的findChildren
.
关于python - 使用 BeautifulSoup Python 拉取相邻的表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33772424/