You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Beautiful Soup爬虫未返回文本求助:爬取ESPN页面无结果

解决ESPN女子篮球计分板爬取不到目标球队的问题

问题根源

  1. find()仅返回第一个匹配元素:你用soup.find()只会拿到页面中第一个带ScoreCell__Competitors类的<ul>,如果"Cleveland State"不在这场比赛里,自然输出内容里不会出现它。
  2. 直接取<ul>文本不精准:该<ul>包含两支球队的信息,直接提取text会把两队名字混在一起,不利于单独定位目标球队。

修正后的代码

from bs4 import BeautifulSoup
import requests

html_text = requests.get('https://www.espn.com/womens-college-basketball/scoreboard/_/date/20221107').text
soup = BeautifulSoup(html_text, 'lxml')

# 获取所有比赛的竞争者列表
all_games = soup.find_all('ul', class_="ScoreCell__Competitors")

# 遍历每一场比赛,提取球队名称
for game in all_games:
    # 定位到存储球队名称的具体标签(实际页面中球队名在TeamName__TeamName类的span里)
    team_names = game.find_all('span', class_="TeamName__TeamName")
    for name in team_names:
        clean_name = name.text.strip()
        print(clean_name)
        # 找到目标球队时直接标记
        if clean_name == "Cleveland State":
            print(f"✅ 找到目标球队:{clean_name}")

额外说明

  • 若仅需查找"Cleveland State",可在匹配到后直接终止循环,节省资源;
  • 爬取ESPN这类网站时,偶尔会遇到页面结构微调,建议用浏览器开发者工具(F12)查看实际HTML结构,确认标签和类名是否正确。

内容的提问来源于stack exchange,提问作者dane w

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 05:15:30