python - 使用 BeautifulSoup Python 拉取相邻的表格单元格

标签 python web-scraping beautifulsoup html-table

table = plan1.find('table', id = 'planComparison')
pcp = table.findChildren(text=' Doctor Visit - Primary Care ')

我使用上面的代码将 pcp 变量设置为“Doctor Visit - Primary Care”单元格。我需要来自旁边单元格的信息(具体情况会有所不同)。

如何拉动相邻的单元格?有没有办法返回 pcp 变量单元格的行号?欢迎任何意见。

Source url

最佳答案

获取下一个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>标签。

另请注意,我指定您想要 tdfind而不是一般的findChildren .

关于python - 使用 BeautifulSoup Python 拉取相邻的表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33772424/

相关文章:

python - Django: 'str'对象在删除具有外键关系的模型时不可调用

python - 将列表字典写入 csv,指定列顺序

python - 使用selenium python从不同的html中获取href标签下的链接

python - 易趣 网页爬虫

python - <span> 内没有类的 BS4 文本

python - 在 jupyter/iPython notebook 脚本和类方法之间同步代码

python - 基本的 Python : How do I normalize a data series?

python - 使用 Python/PhantomJS/Selenium 滚动无限页面

python - 如何使用 playwright-python 处理多个页面?

PHP DOM 属性通配符