gpt4 book ai didi

c# - HTML 敏捷包 : parsing an href tag

转载 作者:可可西里 更新时间:2023-11-01 03:06:28 24 4
gpt4 key购买 nike

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

<tr>
<td rowspan="1" colspan="1">7</td>
<td rowspan="1" colspan="1">
<a class="undMe" href="/ice/player.htm?id=8475179" rel="skaterLinkData" shape="rect">D. Kulikov</a>
</td>
<td rowspan="1" colspan="1">D</td>
<td rowspan="1" colspan="1">0</td>
<td rowspan="1" colspan="1">0</td>
<td rowspan="1" colspan="1">0</td>
[...]

我对播放器 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]);

要真正使用你需要添加错误检查等。

关于c# - HTML 敏捷包 : parsing an href tag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8497673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com