加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 美丽的汤findall多个类使用一个查询

发布时间:2020-12-20 13:49:29 所属栏目:Python 来源:网络整理
导读:我在很多网站和这里彻底搜索了解决方案,但没有一个有效! 我正在尝试刮掉flashscores.com,我想解析一个 td使用类名称cell_ab team-home或cell_ab team-home bold 我尝试过使用re soup.find_all('td',{ 'class'= re.compile(r"^(cell_ab team-home |cell_ab t
我在很多网站和这里彻底搜索了解决方案,但没有一个有效!

我正在尝试刮掉flashscores.com,我想解析一个< td>使用类名称cell_ab team-home或cell_ab team-home bold

我尝试过使用re

soup.find_all('td',{ 'class'= re.compile(r"^(cell_ab team-home |cell_ab team-home  bold )$"))

soup.find_all('td',{ 'class' : ['cell_ab team-home ','cell_ab team-home  bold '])

它们都不起作用.

有人请求代码,所以在这里

from tkinter import *
 from selenium import webdriver
 import threading
 from bs4 import BeautifulSoup

 browser = webdriver.Firefox()
 browser.get('http://www.flashscore.com/')

 HTML = browser.page_source
 soap = BeautifulSoup(HTML)
 for item in soap.find_all('td',class_ = ['cell_ab team-home ','cell_ab team-home  bold ']):
        Listbox.insert(END,item.text)

解决方法

bs4 documentation说下面关于使用class_进行匹配:

Remember that a single tag can have multiple values for its class attribute. When you search for a tag that matches a certain CSS class,you’re matching against any of its CSS classes.

根据文档,您必须使用.select方法在此处使用CSS选择器.因此,这样的事情应该做的伎俩:

soup.select('td.cell_ab.team-home')

这将选择设置了cell_ab和team-home类的所有< td>,包括具有附加类的< td>,例如粗体.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读