【发布时间】:2016-09-25 22:22:46
【问题描述】:
例如:我想查找类“author”(soup.findall(class_='author')) 的元素的内容,但跳过在类“cmets”((soup.findall(class_='comments')) 的元素内搜索。
所以类“作者”但不在任何类“cmets”的元素内
在bs中可以做这样的事情吗?
示例 html:
<div class ='article'>
<span class='author'> John doe</span> <h3>title</h3>
(...)
<div class='comments'>
<div class='row'>
<span class='author'>Whining anon</span>
<div class='content'>
(...)
</div>
</div>
</div>
</div>
【问题讨论】:
-
BS 允许
find_allto take in a function arg as a filter。我在 BS 方面没有太多经验,但你也许可以玩这个。 -
我在路上。我需要
soup.findall(class_='author').findParents()并检查它们是否有“cmets 标签”。但现在我还没准备好去想它。如果到那时没人会的话,我明天会弄清楚。 -
添加html示例
标签: python python-3.x beautifulsoup html-parsing