gpt4 book ai didi

python - 当元素有多个类时,BeautifulSoup SoupStrainer 不起作用?

转载 作者:太空宇宙 更新时间:2023-11-03 19:09:40 24 4
gpt4 key购买 nike

我试试

necessaryStuffOnly = SoupStrainer("table",{"class": "views-table"})
soup = BeautifulSoup(vegetables,parse_only=necessaryStuffOnly)

在这样的 table 上运气不佳:

<div class="view-content">
<table class="views-table sticky-enabled cols-20">
<thead>
<tr>
<td>blablaba</td>
</tr>
</thead>
<tbody>
<tr>
<td>more blablabla</td>
</tr>
</tbody>
</table>
</div>

这对 div 确实有效

SoupStrainer("div",{"class": "view-content"})

SoupStrainer 不能像这样过滤具有多个类的元素吗?

最佳答案

使用的比较是字面相等检查,因此以下内容有效:

soup('table', {'class': "views-table sticky-enabled cols-20"})

您可以通过将函数传递给过滤器来使其匹配:

soup('table', {'class': lambda L: 'views-table' in L.split()})

可能值得检查您正在使用的版本,因为我有一种感觉,这种情况不应该再出现了...更新:是的,给您 https://bugs.launchpad.net/beautifulsoup/+bug/410304

关于python - 当元素有多个类时,BeautifulSoup SoupStrainer 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13413470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com