我想从网页 http://cslh.cz/delegace.html?id_season=2013 的 table class='nice' 中解析日期、链接文本和链接 href
我已经创建了对象 DelegationLink
public class DelegationLink
{
public string date { get; set; }
public string link { get; set; }
public string anchor { get; set; }
}
并将其与 LINQ 一起使用以创建 DelegationLink 列表
var parsedValues =
from table in htmlDoc.DocumentNode.SelectNodes("//table[@class='nice']")
from date in table.SelectNodes("tr//td")
from link in table.SelectNodes("tr//td//a")
.Where(x => x.Attributes.Contains("href"))
select new DelegationLink
{
date = date.InnerText,
link = link.Attributes["href"].Value,
anchortext = link.InnerText,
};
return parsedValues.ToList();
它逐个获取日期列并将其与每一行中的链接列组合,但我只想简单地获取表中的每一行并从该行获取日期、href 和 hreftext。 我是 LINQ 的新手,我使用谷歌 4 小时没有任何效果。感谢您的帮助。
请您参考如下方法:
好吧,这很简单,您只需在 SelectNodes 函数调用中选择 tr 并稍微调整一下您的代码。 像这样。
var parsedValues = htmlDoc.DocumentNode.SelectNodes("//table[@class='nice']/tr").Skip(1)
.Select(r =>
{
var linkNode = r.SelectSingleNode(".//a");
return new DelegationLink()
{
date = r.SelectSingleNode(".//td").InnerText,
link = linkNode.GetAttributeValue("href",""),
anchor = linkNode.InnerText,
};
}
);
return parsedValues.ToList();
