HTML 敏捷包:解析 href 标签
我如何有效地从中解析 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;
}
}
How would I effectively parse the href attribute value from this :
<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>
[...]
I am interested in having the player id, which is: 8475179 Here is the code I have so far:
// 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您的示例,这对我有用:
对于实际使用,您需要添加错误检查等。
Based on your example this worked for me:
For real use you need to add error checking etc.
使用 XPath 表达式来查找它:
Use an XPath expression to find it: