python - BeautifulSoup 使用一个查询找到所有多个类

标签 python beautifulsoup

我在许多网站和此处彻底搜索了解决方案,但没有一个有效!

我正在尝试抓取 flashscores.com,我想解析 <td>与类(class)名称 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)

最佳答案

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.


根据文档,您必须在此处使用 CSS 选择器和 .select方法。因此像这样的东西应该可以解决问题:

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

这将选择所有 <td>同时具有 cell_ab 的和 team-home类(class)集,包括<td>具有附加类的 s,例如 bold .

关于python - BeautifulSoup 使用一个查询找到所有多个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30147223/

相关文章:

python - Flask WTF 到 flask-mail 附件?

python - 计算二维数组中空数组出现的次数

python - Julia:向量化序列的乘积

python - BeautifulSoup .text 方法返回没有分隔符的文本(\n、\r 等)

python - 如何绕过机器人检测并使用 python 抓取网站

python - BeautifulSoup:根据前面标签的内容打印 div

python - 计算唯一对并将计数存储在矩阵中

python - 如何在 Django 表单中创建用户无法编辑的只读字段?

python - 如何通过 dev_appserver.py 在 Google App Engine 上使用 BeautifulSoup 和 lxml

python - 如何在 python 中使用带有代理身份验证的 requests.post()?