相关疑难解决方法(0)

如何使用BeautifulSoup从HTML中删除注释标记?

我一直在玩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)

python beautifulsoup

13
推荐指数
2
解决办法
2万
查看次数

如何在Python中使用Regex删除HTML注释

我想从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)

python regex

5
推荐指数
3
解决办法
4431
查看次数

标签 统计

python ×2

beautifulsoup ×1

regex ×1