在 BeautifulSoup4 的 findAll 中包含多个类名 [英] Include multiple class names in findAll in BeautifulSoup4
本文介绍了在 BeautifulSoup4 的 findAll 中包含多个类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 python 脚本中有一行代码,如下所示
I have a line of code in a python script as shown below
for summaries in soup.findAll('div',{'class':'cb-lv-scrs-col cb-font-12 cb-text-complete'}):
#do something with summaries
但是,我希望摘要中还包括来自 div
项目的项目以及另一个名为 cb-scag-mtch-status cb-text-inprogress
However, i want summaries to also include items from div
items with another class called cb-scag-mtch-status cb-text-inprogress
我已经尝试了以下给出的这里 - BeautifulSoup findAll() 给出了多个类?一个>
I have tried the below as given here - BeautifulSoup findAll() given multiple classes?
for summaries in soup.findAll('div',{'class':['cb-lv-scrs-col cb-font-12 cb-text-complete','cb-scag-mtch-status cb-text-inprogress']}):
#do something with summaries
但这不起作用.有什么问题,我该如何解决?
but this is not working. What is the problem and how do i fix it?
推荐答案
我会做一个简单的CSS 选择器:
soup.select('div[class="cb-lv-scrs-col cb-font-12 cb-text-complete"],div[class="cb-scag-mtch-status cb-text-inprogress"]')
但是,我怀疑您是否真的需要或应该检查元素上存在的所有类,这还不够:
but, I doubt you really need or should check all of the classes present on an element, would not that be sufficient:
soup.select('div.cb-text-complete,div.cb-text-inprogress')
这篇关于在 BeautifulSoup4 的 findAll 中包含多个类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文