HtmlAgilityPack读取数据通过文件流写入到txt中
现在是能读取到数据,但是我创建的log.txt中没有数据,我要实现的是让读取的问题同时写入到txt中
请大神帮忙看看,在线等
int i = 0;
//创建一个htmlweb对象
HtmlWeb web = new HtmlWeb();
web.AutoDetectEncoding = true;
//读取内容返回HtmlAgilityPack.HtmlDocument对象
HtmlAgilityPack.HtmlDocument doc= web.Load(tBtxt_Url.Text.Trim());
HtmlNode node = doc.GetElementbyId("post_list");
//文件流
StreamWriter sw = File.CreateText("log.txt");
foreach (HtmlNode child in node.ChildNodes)
{
if (child.Attributes["class"] == null || child.Attributes["class"].Value != "post_item")
continue;
HtmlNode hn = HtmlNode.CreateNode(child.OuterHtml);
Write(sw, String.Format("推荐:{0}", hn.SelectSingleNode("//*[@class="diggnum"]").InnerText));
Write(sw, String.Format("标题:{0}", hn.SelectSingleNode("//*[@class="titlelnk"]").InnerText));
Write(sw, String.Format("介绍:{0}", hn.SelectSingleNode("//*[@class="post_item_summary"]").InnerText));
Write(sw, String.Format("信息:{0}", hn.SelectSingleNode("//*[@class="post_item_foot"]").InnerText));
string url = hn.SelectSingleNode("//*[@class="titlelnk"]").Attributes["href"].Value.ToString();
Write(sw, String.Format("网址:{0}", url));
Write(sw, "-------------------------------------------------------------------");
i++;
}
sw.Close();