【发布时间】:2016-11-05 04:56:50
【问题描述】:
参考How can I strip comment tags from HTML using BeautifulSoup?,我正在尝试从下面的标签中删除 cmets
>>> h
<h4 class="col-sm-4"><!-- react-text: 124 -->52 Week High/Low:<!-- /react-text --><b><!-- react-text: 126 --> ₹ <!-- /react-text --><!-- react-text: 127 -->394.00<!-- /react-text --><!-- react-text: 128 --> / ₹ <!-- /react-text --><!-- react-text: 129 -->252.10<!-- /react-text --></b></h4>
我的代码 -
comments = h.findAll(text=lambda text:isinstance(text, Comment))
[comment.extract() for comment in comments]
print h
但是搜索 cmets 没有任何结果。我想从上面的标签中提取 2 个值 - "52 Week High/Low:" 和 "₹394.00 / ₹252.10"。
我还尝试使用
从整个 html 中删除标签soup = BeautifulSoup(html)
comments = soup.findAll(text=lambda text:isinstance(text, Comment))
[comment.extract() for comment in comments]
print soup
但是 cmets 仍然存在.. 有什么建议吗?
【问题讨论】:
标签: html python-2.7 tags beautifulsoup bs4