如何用BeautifulSoup从HTML中去除注释标签?
我最近在玩一个叫BeautifulSoup的工具,感觉非常不错。我的最终目标是从网页上提取文本。我主要想从网页的主体部分获取文本,同时还想特别处理一下和标签,提取它们的标题和替代属性。
到目前为止,我的代码是这样的EDITED & UPDATED CURRENT CODE
:
soup = BeautifulSoup(page)
comments = soup.findAll(text=lambda text:isinstance(text, Comment))
[comment.extract() for comment in comments]
page = ''.join(soup.findAll(text=True))
page = ' '.join(page.split())
print page
1) 你有什么建议可以让我在处理这两个标签时,不把它们的属性排除掉吗?如果这个太复杂,那就不那么重要了,我更想解决第二个问题。
2) 我想去掉<!-- -->
标签及其之间的所有内容。我该怎么做呢?
QUESTION EDIT
@jathanism: 这是我尝试去掉的一些注释标签,但即使我使用了你的例子,它们还是没有被去掉。
<!-- Begin function popUp(URL) { day = new Date(); id = day.getTime(); eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=300,height=330,left = 774,top = 518');"); } // End -->
<!-- var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"}); //--> <!-- var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"}); //--> <!-- var whichlink=0 var whichimage=0 var blenddelay=(ie)? document.images.slide.filters[0].duration*1000 : 0 function slideit(){ if (!document.images) return if (ie) document.images.slide.filters[0].apply() document.images.slide.src=imageholder[whichimage].src if (ie) document.images.slide.filters[0].play() whichlink=whichimage whichimage=(whichimage<slideimages.length-1)? whichimage+1 : 0 setTimeout("slideit()",slidespeed+blenddelay) } slideit() //-->
4 个回答
0
如果你在寻找关于BeautifulSoup 3版本的解决方案,可以查看这个链接:BS3文档 - 注释
soup = BeautifulSoup("""Hello! <!--I've got to be nice to get what I want.-->""")
comment = soup.find(text=re.compile("if"))
Comment=comment.__class__
for element in soup(text=lambda text: isinstance(text, Comment)):
element.extract()
print soup.prettify()
66
直接从BeautifulSoup的文档中,你可以很简单地用extract()
来去掉注释(或者其他任何东西):
from BeautifulSoup import BeautifulSoup, Comment
soup = BeautifulSoup("""1<!--The loneliest number-->
<a>2<!--Can be as bad as one--><b>3""")
comments = soup.findAll(text=lambda text:isinstance(text, Comment))
[comment.extract() for comment in comments]
print soup
# 1
# <a>2<b>3</b></a>
3
我还在想为什么它找不到并去掉像这样子的标签:
<!-- //-->
。那些反斜杠导致某些标签被忽略了。
这可能是底层的SGML解析器出现了问题:可以查看这个链接了解更多信息 http://www.crummy.com/software/BeautifulSoup/documentation.html#Sanitizing%20Bad%20Data%20with%20Regexps。你可以通过使用一个markupMassage
的正则表达式来覆盖它——直接来自文档:
import re, copy
myMassage = [(re.compile('<!-([^-])'), lambda match: '<!--' + match.group(1))]
myNewMassage = copy.copy(BeautifulSoup.MARKUP_MASSAGE)
myNewMassage.extend(myMassage)
BeautifulSoup(badString, markupMassage=myNewMassage)
# Foo<!--This comment is malformed.-->Bar<br />Baz