HtmlWeb htmlWeb = new HtmlWeb();
HtmlDocument htmlDoc = htmlWeb.Load(@"http://www.qianzhan.com/");
HtmlNodeCollection uls = htmlDoc.DocumentNode.SelectNodes(@"//div[@id='div-homestocks']/dl/dd/ul");
foreach (HtmlNode ul in uls)
{
foreach (HtmlNode li in ul.Elements("li"))
Response.Write(li.InnerText + " ");
Response.Write("<br/>");
}
Response.End();
下载HtmlAgiltiyPack,引入该程序集,开头添加:
using HtmlAgilityPack; ------其他解决方案-------------------- 改进一下,把涨跌幅分开输出:
HtmlWeb htmlWeb = new HtmlWeb();
HtmlDocument htmlDoc = htmlWeb.Load(@"http://www.qianzhan.com/");
HtmlNodeCollection uls = htmlDoc.DocumentNode.SelectNodes(@"//div[@id='div-homestocks']/dl/dd/ul");
for (int i = 0; i < uls.Count; i++)
{
if (i == 0)
Response.Write("<font color='red'>涨幅排名:</font><br/>");
if (i == uls.Count / 2)
Response.Write("<font color='green'>跌幅排名:</font><br/>");
foreach (HtmlNode li in uls[i].Elements("li"))
Response.Write(li.InnerText + " ");
Response.Write("<br/>");
}
Response.End();