Selenium C#爬取NBA排名数据并导出Excel的技术求助
NBA Standings数据爬取解决方案(Selenium C#)
1. 修正XPath定位错误
你原代码的XPath存在语法问题:linktext不是XPath的合法属性,且缺少闭合括号。正确的文本定位方式如下:
- 精确匹配队名:
IWebElement teamData = Driver.FindElement(By.XPath("//a[text()='Indiana Pacers']"));
- 模糊匹配(应对文本含空格/换行的情况):
IWebElement teamData = Driver.FindElement(By.XPath("//a[contains(text(), 'Indiana Pacers')]"));
2. 区分东西部赛区容器
NBA standings页面的东西部分区有专属标识(如data-conference属性),先定位分区容器再找球队,避免跨区混淆:
// 定位东部赛区容器 IWebElement eastConference = Driver.FindElement(By.XPath("//div[@data-conference='east']")); // 定位西部赛区容器 IWebElement westConference = Driver.FindElement(By.XPath("//div[@data-conference='west']"));
指定球队定位示例
在对应赛区内定位目标球队的整行数据:
// 东部指定Indiana Pacers IWebElement eastTeamRow = eastConference.FindElement(By.XPath(".//a[contains(text(), 'Indiana Pacers')]/ancestor::tr")); // 西部指定Denver Nuggets IWebElement westTeamRow = westConference.FindElement(By.XPath(".//a[contains(text(), 'Denver Nuggets')]/ancestor::tr"));
随机选取球队示例
从赛区内随机选一支球队:
Random rnd = new Random(); // 东部随机球队 List<IWebElement> eastTeamRows = eastConference.FindElements(By.XPath(".//tbody/tr")).ToList(); IWebElement randomEastTeam = eastTeamRows[rnd.Next(eastTeamRows.Count)]; // 西部随机球队 List<IWebElement> westTeamRows = westConference.FindElements(By.XPath(".//tbody/tr")).ToList(); IWebElement randomWestTeam = westTeamRows[rnd.Next(westTeamRows.Count)];
3. 提取核心数据
从球队行中提取队名、胜场、负场、胜率(列索引根据页面结构调整,当前页面胜场为第2列):
// 通用提取方法 string GetTeamData(IWebElement teamRow, string xpath) => teamRow.FindElement(By.XPath(xpath)).Text; // 提取东部球队数据 string eastTeamName = GetTeamData(eastTeamRow, ".//a"); string eastWins = GetTeamData(eastTeamRow, ".//td[2]"); string eastLosses = GetTeamData(eastTeamRow, ".//td[3]"); string eastWinRate = GetTeamData(eastTeamRow, ".//td[4]"); // 西部球队数据同理 string westTeamName = GetTeamData(westTeamRow, ".//a"); string westWins = GetTeamData(westTeamRow, ".//td[2]"); string westLosses = GetTeamData(westTeamRow, ".//td[3]"); string westWinRate = GetTeamData(westTeamRow, ".//td[4]");
4. 保存至Excel
使用EPPlus库操作Excel,先通过NuGet安装EPPlus包,再执行以下代码:
using OfficeOpenXml; using System.IO; // 创建并写入Excel using (var package = new ExcelPackage()) { ExcelWorksheet sheet = package.Workbook.Worksheets.Add("NBA Standings"); // 写入表头 sheet.Cells["A1:E1"].LoadFromArrays(new List<object[]> { new object[] { "赛区", "队名", "胜场", "负场", "胜率" } }); // 写入东部数据 sheet.Cells["A2:E2"].LoadFromArrays(new List<object[]> { new object[] { "东部", eastTeamName, eastWins, eastLosses, eastWinRate } }); // 写入西部数据 sheet.Cells["A3:E3"].LoadFromArrays(new List<object[]> { new object[] { "西部", westTeamName, westWins, westLosses, westWinRate } }); // 保存文件 package.SaveAs(new FileInfo(@"D:\NBA_Standings.xlsx")); }
5. 关键注意事项
- 页面加载需等待:用显式等待确保元素加载完成,避免定位失败:
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10)); IWebElement eastConference = wait.Until(d => d.FindElement(By.XPath("//div[@data-conference='east']")));
- 页面结构更新时,需重新验证XPath的索引和属性。
内容的提问来源于stack exchange,提问作者Scrappy
相关产品推荐
相关产品推荐

