【发布时间】:2019-07-07 14:27:59
【问题描述】:
我在 Python 3.7 中使用 BeautifulSoup 4。我有以下 HTML ...
<tr>
<td class="info"><div class="title">...</div></td>
</tr>
<tr class="ls">
<td colspan="3">Less similar results</td>
</tr>
<tr>
<td class="info"><div class="title">...</div></td>
</tr>
我想提取具有 class="title" 的 DIV,但是,我只想找到出现在表中 TD 文本 =“不太相似的结果”的元素之前的那些。现在我有这个
elts = soup.find("td", class_="info").find_all("div", class_="title")
但这会返回所有具有该类的 DIV,即使是在我要筛选的元素之后发生的 DIV。如何优化我的搜索以仅包含该特定 TD 之前的结果?
【问题讨论】:
-
是否总是在
<tr class="ls">中定义“不太相似的结果”?
标签: django python-3.x beautifulsoup