HTML Agility pack:解析href标记

我如何有效地解析href属性值:

 7  D. Kulikov  D 0 0 0 [...] 

我有兴趣拥有玩家ID,这是: 8475179这是我到目前为止的代码:

  // Iterate all rows (players) for (int i = 1; i < rows.Count; ++i) { HtmlNodeCollection cols = rows[i].SelectNodes(".//td"); // new player Dim_Player player = new Dim_Player(); // Iterate all columns in this row for (int j = 1; j < 6; ++j) { switch (j) { case 1: player.Name = cols[j].InnerText; player.Player_id = Int32.Parse(/* this is where I want to parse the href value */); break; case 2: player.Position = cols[j].InnerText; break; case 3: stats.Goals = Int32.Parse(cols[j].InnerText); break; case 4: stats.Assists = Int32.Parse(cols[j].InnerText); break; case 5: stats.Points = Int32.Parse(cols[j].InnerText); break; } } 

根据你的例子,这对我有用:

 HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.Load("test.html"); var link = htmlDoc.DocumentNode .Descendants("a") .First(x => x.Attributes["class"] != null && x.Attributes["class"].Value == "undMe"); string hrefValue = link.Attributes["href"].Value; long playerId = Convert.ToInt64(hrefValue.Split('=')[1]); 

对于实际使用,您需要添加错误检查等。

使用XPath表达式来查找它:

  foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@class='undMe']")) { HtmlAttribute att = link.Attributes["href"]; Console.WriteLine(new Regex(@"(?<=[\?&]id=)\d+(?=\&|\#|$)").Match(att.Value).Value); }