python - 如何在 python 中使用 BeautifulSoup 移动到标签的末尾?

标签 python tags beautifulsoup

我的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 两次,但每个表中通常有数百个标签,所以这是行不通的。

最佳答案

test = table.nextSibling

为你工作?

关于python - 如何在 python 中使用 BeautifulSoup 移动到标签的末尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8370874/

相关文章:

python - 错误的文件描述符 - Heroku Foreman

emacs - 在 Emacs 中导航重复标签定义的最佳方法是什么?

python - 使用scrapy从无限滚动页面中抓取数据

python - 返回动态创建的函数

tags - CKEditor 去掉 <i> 标签

python - 使用名称从网站上抓取数据表

python - 没有写足够的行到输出文件

python - 培训OpenCV的透明背景

python - Python 中的 N-curses : how to catch and print non ascii character?

c# - 如何使用 C# 从标签 xml 中提取属性?