C# 使用LINQ解析带有HtmlAgilityPack的HTML页面
如何在网页上使用Linq解析html并向字符串添加值。我正在metro应用程序上使用HtmlAgilityPack,希望返回3个值并将它们添加到字符串中 以下是url= 我想从以下内容中获取值,请参见“belwo” “余额:”, “中的交易”, “收到”C# 使用LINQ解析带有HtmlAgilityPack的HTML页面,c#,linq,html-agility-pack,C#,Linq,Html Agility Pack,如何在网页上使用Linq解析html并向字符串添加值。我正在metro应用程序上使用HtmlAgilityPack,希望返回3个值并将它们添加到字符串中 以下是url= 我想从以下内容中获取值,请参见“belwo” “余额:”, “中的交易”, “收到” 您必须解析的文档不是用于解析的格式最完善的文档。许多元素缺少类或至少id属性,但您希望得到的是第二个p标记 内容 你可以试试这个 HtmlDocument htmlDocument = new HtmlDocument(); htmlDocum
您必须解析的文档不是用于解析的格式最完善的文档。许多元素缺少类或至少id属性,但您希望得到的是第二个p标记 内容 你可以试试这个
HtmlDocument htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);
var pNodes = htmlDocument.DocumentNode.SelectNodes("//p")
[1].InnerHtml.ToString().Split(new string[] { "<br />" }, StringSplitOptions.None).Take(3);
string vl="Balance:"+pNodes[0].Split(':')[1]+"Transactions in"+pNodes[1].Split(':')[1]+"Received"+pNodes[2].Split(':')[1];
HtmlDocument HtmlDocument=new HtmlDocument();
htmlDocument.LoadHtml(html);
var pNodes=htmlDocument.DocumentNode.SelectNodes(“//p”)
[1] .InnerHtml.ToString().Split(新字符串[]{“
”},StringSplitOptions.None);
string vl=“Balance:”+pNodes[0]。在“+pNodes[1]中拆分(“:”)[1]+”事务。拆分(“:”)[1]+“收到”+pNodes[2]。拆分(“:”)[1];
请尝试以下操作。你也可以考虑把桌子拉开,因为它比“P”标签中的自由文本好一些。
干杯,亚伦
// download the site content and create a new html document
// NOTE: make this asynchronous etc when considering IO performance
var url = "http://explorer.litecoin.net/address/Li7x5UZqWUy7o1tEC2x5o6cNsn2bmDxA2N";
var data = new WebClient().DownloadString(url);
var doc = new HtmlDocument();
doc.LoadHtml(data);
// extract the transactions 'h3' title, the node we want is directly before it
var transTitle =
(from h3 in doc.DocumentNode.Descendants("h3")
where h3.InnerText.ToLower() == "transactions"
select h3).FirstOrDefault();
// tokenise the summary, one line per 'br' element, split each line by the ':' symbol
var summary = transTitle.PreviousSibling.PreviousSibling;
var tokens =
(from row in summary.InnerHtml.Replace("<br>", "|").Split('|')
where !string.IsNullOrEmpty(row.Trim())
let line = row.Trim().Split(':')
where line.Length == 2
select new { name = line[0].Trim(), value = line[1].Trim() });
// using linqpad to debug, the dump command drops the currect variable to the output
tokens.Dump();
//下载网站内容并创建新的html文档
//注意:在考虑IO性能时,将此设置为异步etc
变量url=”http://explorer.litecoin.net/address/Li7x5UZqWUy7o1tEC2x5o6cNsn2bmDxA2N";
var data=new WebClient().DownloadString(url);
var doc=新的HtmlDocument();
doc.LoadHtml(数据);
//提取事务“h3”标题,我们想要的节点就在它前面
变量transTitle=
(来自doc.DocumentNode.substands(“h3”)中的h3)
其中h3.InnerText.ToLower()=“事务”
选择h3.FirstOrDefault();
//标记摘要,每个“br”元素一行,每行用“:”符号分隔
var summary=transTitle.PreviousSibling.PreviousSibling;
变量标记=
(来自summary.InnerHtml.Replace(“
”,“|”)中的行。拆分(“|”)
其中!string.IsNullOrEmpty(row.Trim())
让line=row.Trim().Split(“:”)
其中line.Length==2
选择新的{name=line[0].Trim(),value=line[1].Trim()});
//使用linqpad进行调试,dump命令将currect变量放到输出中
tokens.Dump();
“Dump()”是一个LinqPad命令,用于将变量转储到控制台,下面是Dump命令的输出示例:
- 结余:5 LTC
- 内地的交易:2宗
- 收到:5 LTC
- 交易额:0
- 已发送:0 LTC
// download the site content and create a new html document
// NOTE: make this asynchronous etc when considering IO performance
var url = "http://explorer.litecoin.net/address/Li7x5UZqWUy7o1tEC2x5o6cNsn2bmDxA2N";
var data = new WebClient().DownloadString(url);
var doc = new HtmlDocument();
doc.LoadHtml(data);
// extract the transactions 'h3' title, the node we want is directly before it
var transTitle =
(from h3 in doc.DocumentNode.Descendants("h3")
where h3.InnerText.ToLower() == "transactions"
select h3).FirstOrDefault();
// tokenise the summary, one line per 'br' element, split each line by the ':' symbol
var summary = transTitle.PreviousSibling.PreviousSibling;
var tokens =
(from row in summary.InnerHtml.Replace("<br>", "|").Split('|')
where !string.IsNullOrEmpty(row.Trim())
let line = row.Trim().Split(':')
where line.Length == 2
select new { name = line[0].Trim(), value = line[1].Trim() });
// using linqpad to debug, the dump command drops the currect variable to the output
tokens.Dump();