簡體   English   中英

如何在python中使用BeautifulSoup移動到標記的末尾?

[英]How can I move to the end of a tag using BeautifulSoup in python?

我有如下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

為你工作?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM