【发布时间】:2015-02-24 14:24:30
【问题描述】:
我正在使用 BS 库进行 HTML 解析。我的任务是删除头部标签之间的所有内容。所以如果我有<head> A lot of Crap! </head>,那么结果应该是<head></head>。这是它的代码
raw_html = "entire_web_document_as_string"
soup = BeautifulSoup(raw_html)
head = soup.head
head.unwrap()
print(head)
这很好用。但我希望这些更改应该发生在包含整个 html 文档的 raw_html 字符串中。如何在原始字符串中反映这些命令,而不仅仅是在head 字符串中?你能分享一个代码sn-p吗?
【问题讨论】:
-
如果要清除标签,请使用
tag.clear()
标签: python html parsing beautifulsoup html-parsing