最近自己找了一个开源的博客网站,放到阿里云上,方便自己发布博客。

我一般把文章发布到博客园和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内容的更多相关文章

  1. 获取txt文件指定行内容

    #!/usr/bin/python num=0; ni=open("C:\Python34\ceshi.txt") for line in ni: num=num+1;  #表示行 ...

  2. python读取文件指定行内容

    python读取文件指定行内容 import linecache text=linecache.getline(r'C:\Users\Administrator\Desktop\SourceCodeo ...

  3. php获取指定div内容

    <?php $p="http://127.0.0.1:8080/website/index.html"; $ch = curl_init(); curl_setopt($ch ...

  4. Git 获取远程仓库指定分支内容

    1. 在本地一个空的文件夹中 git init  (生成本地仓库) 2. 在刚刚的文件夹中随便建立一个文件 ,git add . (为了生成分支)(提交到暂存区) 3. git commit -m'1 ...

  5. springboot获取properties文件的配置内容(转载)

    1.使用@Value注解读取读取properties配置文件时,默认读取的是application.properties. application.properties: demo.name=Name ...

  6. 一种获取xml文件某个节点内容的shell方法

    配置文件 config.xml <xml> <server> <name>srv-01</name> </server> <serve ...

  7. js调用浏览器打印指定div内容

    --打印按钮事件 function printForm(){    var headstr = '<html xmlns:th="http://www.thymeleaf.org&qu ...

  8. 关于MVC打印问题,打印指定的内容

    首先你的内容一定要放在一个div中如下代码 <div id="divprint"> <table class="table table-striped ...

  9. python3读取文件指定行的三种方案

    技术背景 考虑到深度学习领域中的数据规模一般都比较大,尤其是训练集,这个限制条件对应到实际编程中就意味着,我们很有可能无法将整个数据文件的内容全部都加载到内存中.那么就需要一些特殊的处理方式,比如:创 ...

随机推荐

  1. CentOS6.5菜鸟之旅:安装Realtek无线网卡驱动

    一.前言 CentOS6.5不像CentOS7和Unbuntu那样自动安装好了无线网卡驱动,因此需要我们折腾一下. 二.安装前的准备工作   [a] 检查无线网卡驱动的安装情况(通过查看网络接口的安装 ...

  2. C# 文字转声音

    添加COM组件引用:Microsoft Speech object library private SpVoice voice; private void button1_Click(object s ...

  3. java版复利计算器升级

    github地址:https://github.com/iamcarson/Carson 伙伴:彭宏亮 学号:201406114148 与伙伴工作帅照: 本次升级的地方: 1.改善了界面显示,让界面整 ...

  4. Web 前端性能优化准则

    准则01:尽量减少http请求 “只有10%-20%的最终用户响应时间花在接收请求的HTML文档上,剩下的80%-90%时间花在HTML文档所引用的所有组件(图片,script,css,flash等等 ...

  5. 基于android平台的斗地主AI

    本软件是基于android平台的斗地主AI,我们在源代码的基础之上,旨在改进AI的算法,使玩家具有更丰富的体验感,让NPC可以更为智能. (一)玩法解析: (1)发牌和叫牌:一副扑克54张,先为每个人 ...

  6. 译:什么是ViewData的, ViewBag和TempData? - MVC为当前和后续请求之间传递数据的三种方法

    译文出处:http://www.codeproject.com/Articles/476967/WhatplusisplusViewData-cplusViewBagplusandplusTem AS ...

  7. 【iOS】利用Runtime特性做监控

    最近在看Object-C运行时特性,其中有一个特别好用的特性叫 Method Swizzling ,可以动态交换函数地址,在应用程序加载的时候,通过运行时特性互换两个函数的地址,不改变原有代码而改变原 ...

  8. 【iOS】FMDB封装,查询自动mapping

    sqlite几乎所有的App都会用到,但是系统自带的sqlite API是用C语言写的,非常不友好,用起来非常不便,通常我们使用第三方封装好的工具,例如:FMDB(https://github.com ...

  9. DataList分页访问FooterTemplate模板里的控件

    今天做DataList分页的时候,突然想把分页控件写在FooterTemplate模板里面,弄了很久都访问不到控件,终于发现问题所在,以下是访问FooterTemplate里控件的方法: <Fo ...

  10. 重新想象 Windows 8 Store Apps (56) - 系统 UI: Scale, Snap, Orientation, High Contrast 等

    [源码下载] 重新想象 Windows 8 Store Apps (56) - 系统 UI: Scale, Snap, Orientation, High Contrast 等 作者:webabcd ...