在 find_all 中获取第二个标签

Obtain second tag inside a find_all

我正在尝试获取特定 td 内的第二个标签,但我无法仅获取第二个标签的文本,因为我正在从所有 a. 后面我会做一个for获取10td的数据。正如您在图像中看到的,我想要 10 个 td 中的每一个中的第二个 a 的数据:

我的代码:

from requests import get
from bs4 import BeautifulSoup

headers = {'User-Agent': 'Mozilla/5.0'}

url = 'https://www.oddsportal.com/soccer/spain/laliga'
response = get(url, headers=headers)

html_soup = BeautifulSoup(response.text, 'html.parser')
type(html_soup)



match_containers = html_soup.find_all("td",{ "class" : "name table-participant"})

print(len(match_containers))

first_match = match_containers[0]

first_title = first_match.text
print (first_title)

第二个 a 标签需要 select

import requests
from bs4 import BeautifulSoup as bs

url = 'https://www.oddsportal.com/soccer/spain/laliga'
r = requests.get(url, headers = {'User-Agent' : 'Mozilla/5.0'})
soup = bs(r.content, 'lxml')
print([item.text for item in soup.select('#tournamentTable tr[xeid] [href*=soccer]')])

尽管您可以删除 table id 并使用:

print([item.text for item in soup.select('tr[xeid] [href*=soccer]')])

对于 table 的行,以有用的匹配数据作为列表,我将使用:

rows = soup.select('#tournamentTable tr[xeid]')