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

时间:2015-05-10 02:52:18

标签: python beautifulsoup

我在很多网站和这里都彻底搜索了解决方案,但没有一个能够正常工作!

我正在尝试抓取flashscores.com,我想解析一个<td>类名cell_ab team-homecell_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)

5 个答案:

答案 0 :(得分:2)

bs4 documentation说明了使用class_匹配的以下内容:

  

请记住,单个标记可以为其class属性提供多个值。当您搜索与某个CSS类匹配的标记时,您将匹配其任何CSS类。

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

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

这将选择同时设置了<td>cell_ab类的所有team-home,包括<td>个具有其他类的bold,例如ehlo

答案 1 :(得分:0)

您可以使用re找到它:

soap.findAll('td', {'class' : re.compile('cell_ab team-home '|'cell_ab team-home  bold ')})

这会找到标记为td的标记为class='cell_ab team-home',标记为td的标记为clas='cell_ab team-home bold'

答案 2 :(得分:0)

您可以使用列表sitax,例如:

h:m:i

答案 3 :(得分:0)

table = soup.find_all("tr",class_=["odd","even"])

此解决方案对我有用!确保使用正确的括号和花括号!

答案 4 :(得分:-1)

你可以像这样使用选择器:

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