C#获取HTML文件指定DIV内容
2016-11-04 15:30
645 查看
最近自己找了一个开源的博客网站,放到阿里云上,方便自己发布博客。
我一般把文章发布到博客园和QQ空间,家了这个网站后又要多发布一次,为了省事就做了一个从博客园读取文章的功能;
输入链接URL地址点击提交;
c.Encoding = Encoding.GetEncoding("UTF-8");
string html = c.DownloadString(urlStr);
通过WebClient读取网页,注意这里的编码问题,有的网页用的是UTF-8有的是GB2312
自己尝试一下就知道了,编码设置错误会出现汉子乱码。
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
HtmlNode nodeinfo = doc.GetElementbyId("post_detail");
读取HTML字符串中指定id的标签的内容;
参考博客:http://www.cnblogs.com/ITmuse/archive/2010/05/29/1747199.html
最终我不用再重复添加博客了,不过还多亏博客园没有做图片防盗链,否则图片还要单独处理。
我一般把文章发布到博客园和QQ空间,家了这个网站后又要多发布一次,为了省事就做了一个从博客园读取文章的功能;
输入链接URL地址点击提交;
从GetHub安装HtmlAgilityPack
后台C#代码
public ActionResult LinkBlog(string urlStr) { Response response = new Response() { Code =1 }; if (string.IsNullOrWhiteSpace(urlStr)) { response.Code = 0; response.Message = "链接URL必填"; return Json(response); } WebClient c = new WebClient(); c.Encoding = Encoding.GetEncoding("UTF-8"); string html = c.DownloadString(urlStr); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); HtmlNode nodeinfo = doc.GetElementbyId("post_detail"); //post_detail // HtmlNode nodetitle = doc.GetElementbyId("cb_post_title_url"); //cnblogs_post_body HtmlNode nodecontent = doc.GetElementbyId("cnblogs_post_body"); string htmlstr = nodeinfo.OuterHtml; Blog blog = new Blog(); blog.Publish = true; blog.Title =string.Format("链接文章:{0}", nodetitle.InnerText); blog.Volume = 0; blog.Content = htmlstr; blog.CreateTime = DateTime.Now; string htmlsumm = nodecontent.InnerText.Replace(" ", ""); int sublen = htmlsumm.Length; if (sublen > 80) { sublen = 80; } blog.Summary = htmlsumm.Substring(0, sublen); blog.Category= categoryManager.FindRoot()[0]; response = blogManager.AddBlog(blog); return Json(response); }
应用的技术
WebClient c = new WebClient();c.Encoding = Encoding.GetEncoding("UTF-8");
string html = c.DownloadString(urlStr);
通过WebClient读取网页,注意这里的编码问题,有的网页用的是UTF-8有的是GB2312
自己尝试一下就知道了,编码设置错误会出现汉子乱码。
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
HtmlNode nodeinfo = doc.GetElementbyId("post_detail");
读取HTML字符串中指定id的标签的内容;
参考博客:http://www.cnblogs.com/ITmuse/archive/2010/05/29/1747199.html
最终我不用再重复添加博客了,不过还多亏博客园没有做图片防盗链,否则图片还要单独处理。
相关文章推荐
- HTML to Image in C#指定网页地址,获取内容为图片
- C# 正则表达式获取指定标签的内容
- js操作cookie;js的setInterval;C#获取指定页面的内容;Ajax.dll的使用
- C#获取Word文档页数,并跳转到指定的页面获取内容,且插入分页符
- c#正则匹配指定地址指定div内容
- C# 获取指定HTML网页中的标签内容
- C#获取网页指定内容
- C#获取网页内容
- c#远程获取网页内容
- c#远程获取网页内容及乱码问题的解决办法
- C#获取指定URL页面的HTML源码
- [C#]获取某年指定周的开始日期和结束日期的通用方法
- 如何用C#获取指定文件夹下所有文件名?
- 将指定的asp文件内容生成HTML文件
- 在C#中修改Web.Config的指定内容
- 正则表达式获取HTML标记中的内容(C#)
- C#获取网页内容
- 获取UltraWebTree指定节点的所有父节点内容
- C#获取图片的指定部分
- DataGrid相邻行有相同内容时对指定列合并和C#可以实现DLL库的动态调用