C#获取HTML文件指定DIV内容
最近自己找了一个开源的博客网站,放到阿里云上,方便自己发布博客。
我一般把文章发布到博客园和QQ空间,家了这个网站后又要多发布一次,为了省事就做了一个从博客园读取文章的功能;
输入链接URL地址点击提交;

从GetHub安装HtmlAgilityPack

后台C#代码
public ActionResult LinkBlog(string urlStr)
{
Response response = new Response() { Code = };
if (string.IsNullOrWhiteSpace(urlStr))
{
response.Code = ;
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 = ;
blog.Content = htmlstr;
blog.CreateTime = DateTime.Now;
string htmlsumm = nodecontent.InnerText.Replace(" ", "");
int sublen = htmlsumm.Length;
if (sublen > )
{
sublen = ;
}
blog.Summary = htmlsumm.Substring(, sublen); blog.Category= categoryManager.FindRoot()[];
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

最终我不用再重复添加博客了,不过还多亏博客园没有做图片防盗链,否则图片还要单独处理。
C#获取HTML文件指定DIV内容的更多相关文章
- 获取txt文件指定行内容
#!/usr/bin/python num=0; ni=open("C:\Python34\ceshi.txt") for line in ni: num=num+1; #表示行 ...
- python读取文件指定行内容
python读取文件指定行内容 import linecache text=linecache.getline(r'C:\Users\Administrator\Desktop\SourceCodeo ...
- php获取指定div内容
<?php $p="http://127.0.0.1:8080/website/index.html"; $ch = curl_init(); curl_setopt($ch ...
- Git 获取远程仓库指定分支内容
1. 在本地一个空的文件夹中 git init (生成本地仓库) 2. 在刚刚的文件夹中随便建立一个文件 ,git add . (为了生成分支)(提交到暂存区) 3. git commit -m'1 ...
- springboot获取properties文件的配置内容(转载)
1.使用@Value注解读取读取properties配置文件时,默认读取的是application.properties. application.properties: demo.name=Name ...
- 一种获取xml文件某个节点内容的shell方法
配置文件 config.xml <xml> <server> <name>srv-01</name> </server> <serve ...
- js调用浏览器打印指定div内容
--打印按钮事件 function printForm(){ var headstr = '<html xmlns:th="http://www.thymeleaf.org&qu ...
- 关于MVC打印问题,打印指定的内容
首先你的内容一定要放在一个div中如下代码 <div id="divprint"> <table class="table table-striped ...
- python3读取文件指定行的三种方案
技术背景 考虑到深度学习领域中的数据规模一般都比较大,尤其是训练集,这个限制条件对应到实际编程中就意味着,我们很有可能无法将整个数据文件的内容全部都加载到内存中.那么就需要一些特殊的处理方式,比如:创 ...
随机推荐
- 0527Sprint总结,读书笔记与提问
第八章主要软件团队如何准确全面的找到人们对于软件五花八门的需求,主要有以下几个步骤: 1.获取和引导需求 2.分析和定义需求 3.验证需求 4.在软件产品的生命周期中管理需求 . 接下来讲了软件产品的 ...
- 转载---CSS3实现曲线阴影和翘边阴影
预备知识 DIV+CSS基础 圆角:border-radius 2D变换:transform:skew && rotate 伪类::before 和 :after 代码 HTML结构代 ...
- LeetCode - 30. Substring with Concatenation of All Words
30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...
- 微软参考源代码 referencesource.microsoft.com
微软参考源代码 http://referencesource.microsoft.com/ referencesource 下载地址 https://github.com/Microsoft/refe ...
- 你得知道这3个最基础的APP技术框架
出处:优设网作者:信籽链接:http://www.uisdc.com/3-basic-app-technical-framework 信籽(手淘设计师):不知道大家有没有遇到过这种情景,当你做好一个设 ...
- ref与out的区别
若要使用 ref 参数,方法定义和调用方法均必须显式使用 ref 关键字,如下面的示例所示. class RefExample { static void Method(ref int i) { // ...
- js用正则表达式验证用户和密码的安全性,生成随机验证码
制作了一个表单,表单验证用户.密码.随机验证码 html页面
- Jquery的画图插件-jqPlot+
JqPlot(个人和商业都免费) http://yunpan.cn/Q7Yh5dUNPjtak 提取码 3653 下面开始制作一个柱状图: ----------------------------我 ...
- 整合spring,springmvc和mybatis
我创建的是maven项目,使用到的依赖架包有下面这些: <dependencies> <dependency> <groupId>org.springframewo ...
- Bootstrap 框架 栅格布局系统设计原理
如果你是初次接触Bootstrap,你一定会为它的栅格布局感到敬佩.事实上,这个布局系统提供了一套响应式的布局解决方案. 既然这么好用,那他是如何用CSS来实现的呢? 我特意去Bootstrap官方下 ...