现在是能读取到数据,但是我创建的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();private void Write(StreamWriter writer, string str)
{
if (this.InvokeRequired)
{
ShowText st = new ShowText(Write);
this.Invoke(st, str);
}
else
{
listBox_Content.Items.Add(str);
}c#ioHtmlAgilityPack
请大神帮忙看看,在线等
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();private void Write(StreamWriter writer, string str)
{
if (this.InvokeRequired)
{
ShowText st = new ShowText(Write);
this.Invoke(st, str);
}
else
{
listBox_Content.Items.Add(str);
}c#ioHtmlAgilityPack
解决方案 »
- sqlite数据库 如何添加无符号值类型
- 如何写批处理文件调用Installshield 编译
- listview datagrid 等常见的问题
- 如何复制一个窗体到另一个工程中?
- 【折腾了我几天的问题】 用WebRequest采用多线程抓取网页时,出现"基础链接已关闭"的错误,大家帮分析一下!!!!!!!!!!
- Winform中DataGridView对原对象修改后,如何取消找回原对象?
- 想找一本用VISUAL C#.NET的中文电子书!谁知道那有啊???
- 小小问题,不过值得一看
- 如何屏蔽键盘上的所有按键?
- HTTP Error 500.0 - Internal Server Error 您查找的资源存在问题,因而无法显示。
- C#获得当前窗体的标题
- C# 3DES加密解密
sw.write() 不行?