当前位置: 移动技术网 > IT编程>开发语言>.net > 解析xHTML源码的DLL组件AngleSharp介绍

解析xHTML源码的DLL组件AngleSharp介绍

2017年12月12日  | 移动技术网IT编程  | 我要评论

赵总理,麻仓优下马番号,热火雄鹿

anglesharp是基于.net(c#)开发的专门为解析xhtml源码的dll组件。

项目地址:https://github.com/florianrappl/anglesharp

我主要介绍是一些使用anglesharp常用的方法,跟大家介绍,我会以移动技术网站点作为原型。 其它的类似组件有:

国内:jumony
github地址: https://github.com/ivony/jumony

国外:html agility pack
项目地址:

具体大家可以自行搜索对比三者的区别和性能。接下来咱们主要讨论主角是anglesharp

引入anglesharp至项目,用nuget工具执行命令(其实我是在装逼。) install-package anglesharp

在项目中添加引用using anglesharp

首先我们获取cnblogs首页的html源代码

static public string gethtml()
{
  httpwebrequest myreq =
  (httpwebrequest)webrequest.create("//www.jb51.net");
  httpwebresponse response = (httpwebresponse)myreq.getresponse();
  // get the stream associated with the response.
  stream receivestream = response.getresponsestream();

  // pipes the stream to a higher level stream reader with the required encoding format. 
  streamreader readstream = new streamreader(receivestream, encoding.utf8);

  return readstream.readtoend();
}

获取jb51首页当前所有博客文章的标题

private static void main(string[] args) { //找出所有文章标题 string cnblogshtml = gethtml();

  //加载html
  var document = documentbuilder.html(cnblogshtml);
  //这里必须要使用== 不能使用equals
  var titleitemlist = document.all.where(m => m.classname == "titlelnk");
  int iindex = 1;
  foreach (var element in titleitemlist)
  {
    console.writeline(iindex + ":" + element.innerhtml);
    iindex++;
  }
}

以上代码输出内容:

1:jndi学习总结(三)——tomcat下使用druid配置jndi数据源
2:我们前端是怎么跟设计师沟通的
3:mvc5+ef6 入门完整教程六
4:试议常用javascript 类库中 throttle 与 debounce 辅助函数的区别
5:孤独的走过年轻
6:上周热点回顾(11.10-11.16)
7:android动画-补间(tween)动画
8:朴素贝叶斯算法的python实现
9:mvc三层级联方式
10:c# 标签(条码)的打印与设计(一)
11:opencascade make primitives-box
12:基于solr实现hbase的二级索引
13:(十六)webgis中偏移补偿量引发的问题之探讨
14:javascript小游戏--生命游戏
15:android动画-帧动画
16:c# socket学习笔记一
17:lua表排序
18:zookeeper系列 第一篇:zookeeper快速入门
19:【插件开发】—— 9 编辑器代码分块着色-高亮显示!
20:华盛顿大学计算机视觉课程笔记(一)

官方有提供详细的文档和例子,大家可以去看一下。此插件最大的优势:支持输出javascript、linq语法、id和class选择器、动态添加节点。实为.net开发之利器。

anglesharp文档:https://github.com/florianrappl/anglesharp/wiki/documentation

anglesharp例子(demo):https://github.com/florianrappl/anglesharp/wiki/examples

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网