如何从Baseball-Reference抓取科罗拉多相关棒球运动员指定数据?
解决方案:Baseball-Reference科罗拉多州相关棒球运动员数据抓取
定位目标数据容器:Baseball-Reference的球员核心信息都在
div.infobox.vcard容器内,先锁定这个容器能避免抓取大量无关内容:infobox = soup.find('div', class_='infobox vcard')提取球员姓名:容器内的
h1标签直接存储了球员全名,可直接提取:player_name = infobox.find('h1').text.strip()提取出生地并判断归属:找到包含"Born:"的标签,提取对应地点文本后检查是否包含"Colorado":
born_info = infobox.find('span', string='Born:') is_colorado_born = False birth_place = '未知' if born_info: # 跳过括号内的日期,只保留地点部分 birth_place = born_info.find_next_sibling(text=True).strip().split('(')[0].strip() is_colorado_born = 'Colorado' in birth_place提取高中信息并判断归属:找到标注"High School"的表头,提取对应单元格文本后检查是否包含科罗拉多州:
high_school_info = infobox.find('th', string='High School') is_colorado_hs = False if high_school_info: hs_text = high_school_info.find_next('td').text.strip() # 高中信息格式通常为"学校名, 城市, 州",通过", Colorado"判断归属 if ', Colorado' in hs_text: is_colorado_hs = True整合数据到字典:根据判断结果将球员信息分类存入对应字典:
Colorado_Born_and_HS = {} Colorado_Born = {} Colorado_HS = {} if is_colorado_born and is_colorado_hs: Colorado_Born_and_HS[player_name] = birth_place elif is_colorado_born: Colorado_Born[player_name] = birth_place elif is_colorado_hs: Colorado_HS[player_name] = birth_place完整可运行代码:
import requests from bs4 import BeautifulSoup URL = 'https://www.baseball-reference.com/players/p/paytoja01.shtml' r = requests.get(URL) soup = BeautifulSoup(r.content, 'html5lib') # 定位核心信息容器 infobox = soup.find('div', class_='infobox vcard') if not infobox: print("未找到球员信息容器") exit() # 提取球员姓名 player_name = infobox.find('h1').text.strip() # 处理出生地信息 born_info = infobox.find('span', string='Born:') is_colorado_born = False birth_place = '未知' if born_info: birth_place = born_info.find_next_sibling(text=True).strip().split('(')[0].strip() is_colorado_born = 'Colorado' in birth_place # 处理高中信息 high_school_info = infobox.find('th', string='High School') is_colorado_hs = False if high_school_info: hs_text = high_school_info.find_next('td').text.strip() if ', Colorado' in hs_text: is_colorado_hs = True # 分类存储数据 Colorado_Born_and_HS = {} Colorado_Born = {} Colorado_HS = {} if is_colorado_born and is_colorado_hs: Colorado_Born_and_HS[player_name] = birth_place print(f"球员 {player_name} 出生于科罗拉多州且在该州读高中,已存入Colorado_Born_and_HS") elif is_colorado_born: Colorado_Born[player_name] = birth_place print(f"球员 {player_name} 出生于科罗拉多州,已存入Colorado_Born") elif is_colorado_hs: Colorado_HS[player_name] = birth_place print(f"球员 {player_name} 在科罗拉多州读高中,已存入Colorado_HS") else: print(f"球员 {player_name} 与科罗拉多州无关")
关键提示
- 不要盲目抓取所有标签,先定位核心信息容器能大幅减少无效数据
- 用
string参数匹配标签内的文本,比单纯依赖属性定位更精准 - 要处理数据缺失的情况(部分球员可能无高中信息),避免程序报错
内容的提问来源于stack exchange,提问作者Brandon Smith
相关产品推荐
相关产品推荐

