我一直在玩BeautifulSoup,这很棒.我的最终目标是尝试从页面中获取文本.我只是想从正文文本,用特制的情况下拿到冠军和/或ALT属性从<a>或<img>标签.
到目前为止我有这个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
Run Code Online (Sandbox Code Playgroud)
1)你有什么建议我的特殊情况的最好方法是不从上面列出的两个标签中排除这些属性?如果它太复杂而不能做到这一点,那就不像做#2那么重要了.
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 …Run Code Online (Sandbox Code Playgroud) 我想从HTML文本中删除HTML注释
<h1>heading</h1> <!-- comment-with-hyphen --> some text <-- con --> more text <hello></hello> more text
Run Code Online (Sandbox Code Playgroud)
应导致:
<h1>heading</h1> some text <-- con --> more text <hello></hello> more text
Run Code Online (Sandbox Code Playgroud)