【发布时间】:2023-01-12 07:58:18
【问题描述】:
目前,我这样做是为了仅当 div 中有一个 tf-match-analyst-verdict 元素时才传递,而 div 又应该包含一个名为 match-header 的 class:
matches = soup.find_all('div', attrs={"class": "match-header"})
for match in matches:
if (match.find('tf-match-analyst-verdict')):
在创建 matches 对象时传递此需求以消除使用 if 的需求的正确方法是什么?
【问题讨论】:
-
如果BS4的选择器语法支持
:has(),可以使用soup.select('div.match-header:has(tf-match-analyst-verdict)') -
嗨@Barmar 它完美地解决了我的需求
标签: python beautifulsoup