【问题标题】:How can I move to the end of a tag using BeautifulSoup in python?如何在 python 中使用 BeautifulSoup 移动到标签的末尾?
【发布时间】:2012-01-12 08:11:05
【问题描述】:

我的html如下:

html = '<html><table>this is a table<p>some text</p></table><p>text outside of table</p></html>'

我想移动到表格的末尾,然后找到下一个标签。我尝试使用 findNext,但如果表内有标签,它会找到该标签而不是表外的下一个标签。

soup = BeautifulSoup(''.join(text))
table = soup.find('table')
test = table.findNext()

这段代码给了我:

<p>some text</p>

但是,我希望它给我:

<p>text outside of table</p>

主要问题是我不能总是指定一个标签是一个'p'标签。我可以有这样的html:

html = '<html><table>this is a table<td>some text</td></table><table>text outside of table</table></html>'

所以,我不能真正依赖标签标识符来获得下一个。在上面的代码中,我想返回:

<table>text outside of table</table>

我意识到我可以只使用 findNext 两次,但通常每个表中都有数百个标签,所以这不起作用。

【问题讨论】:

    标签: python tags beautifulsoup


    【解决方案1】:

    会

    test = table.nextSibling
    

    为你工作?

    【讨论】:

    • 哇,我觉得自己很笨。我尝试使用 findNextSibling 但它不起作用。我想我应该更仔细地阅读文档。谢谢。
    猜你喜欢
    • 2011-02-13
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 2011-12-17
    • 2019-09-15
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多