例如抓主页中的阅读量 class=s1
的span
,无法获取此节点,而s2
的则无内容。
请问这是什么问题呢?如何解决?谢谢。
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load("http://weixin.sogou.com");
HtmlNode node = doc.GetElementbyId("pc_0_0");//输出node中找不到span_s1节点
StreamWriter sw = File.CreateText("log.txt");
foreach (HtmlNode child in node.ChildNodes)
{
if (child.Name != "li")
continue;
HtmlNode hn = HtmlNode.CreateNode(child.OuterHtml);
Write(sw, String.Format("公众号:{0}", hn.SelectSingleNode("//*[@class=\"account\"]").InnerText));
Write(sw, String.Format("地址:{0}", hn.SelectSingleNode("//*[@class=\"account\"]").Attributes["href"].Value));
Write(sw, String.Format("标题:{0}", hn.SelectSingleNode("//h3").InnerText));
Write(sw, String.Format("地址:{0}", hn.SelectSingleNode("//h3").SelectSingleNode("//a").Attributes["href"].Value));
Write(sw, String.Format("缩略图:{0}", hn.SelectSingleNode("//*[@class=\"img-box\"]").SelectSingleNode("//img").Attributes["src"].Value));
//Write(sw, String.Format("阅读量:{0}", hn.SelectSingleNode("//*[@class=\"s1\"]").InnerText)); //节点找不到
Write(sw, String.Format("简介:{0}", hn.SelectSingleNode("//*[@class=\"txt-info\"]").InnerText));
}
sw.Close();
Console.ReadLine();