我在许多网站和此处彻底搜索了解决方案,但没有一个有效!
我正在尝试抓取 flashscores.com,我想解析 <td>
与类(class)名称 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.
根据文档,您必须在此处使用 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/