我正在将beautifulsoup4与python3.7结合使用。我想找到一个元素,它的元素中有文本“points”,但也有一个类属性包含“article”的祖先DIV。我已经知道如何用文本搜索元素。。。你知道吗
points_elt = soup.find_all(text=re.compile(' points'))[0]
但是我不知道如何扩展上面的内容来包含文本中的元素,这些元素也包含类“article”的祖先。。你知道吗
<div class="article class2">
... other elements ...
<span class="outerSpan">
<span class="innerSpan">2000 points</span>
</span>
... other element closing tags ...
</div>
这是另一个例子,它应该工作。。。你知道吗
<div class="article class7">
<p>
<div class="abc">
<span class="outerSpan">
<span>8000 points</span>
</span>
</div>
</p>
</div>
输出:
输出:
span变量包含所有span元素,我们将遍历回
HTML
标记的父级。考虑到这总是HTML
的格式。你知道吗您可以使用css选择器并检查您正在处理的字符串。你知道吗
或者你可以用这个。你知道吗
相关问题 更多 >
编程相关推荐