【发布时间】:2021-02-13 15:54:21
【问题描述】:
我有一个 html 文件,其中包含多个 div 并且没有类或 id 名称:
<div><span>Item 1</span></div>
<div>some content</div>
<div><table><tbody>table content</tbody></table></div>
<div><span>Item 2</span></div>
<div>some content</div>
<div><table><tbody>table content</tbody></table></div>
<div><span>Item 3</span></div>
<div>some content</div>
<div><table><tbody>table content</tbody></table></div>
<div><span>Item 4</span></div>
<div>some content</div>
<div><table><tbody>table content</tbody></table></div>
目标:
我只想在 Item 3 之后获取 table 的内容,并且搜索是一般性的,以获取任何 table after 特定 Item数字。请注意,项目的数量可能会有所不同。
试过了:
我试过这段代码:
if soup.find('span') == 'Item 3. Selected Financial Data':
table_tag = soup.find('tbody')
但它仍然返回从顶部开始的第一个表。
我还尝试查看 .next_siblings 和 .next_element 但没有更好的方法来实现这一点吗?
https://www.crummy.com/software/BeautifulSoup/bs4/doc/#going-sideways
这里有一个类似的问题:get text after specific tag with beautiful soup,但它不涉及 div 中的表。
请帮忙。
【问题讨论】:
标签: python html beautifulsoup