当我们需要解析一个web页面的时候,如果非常简单,可以用字符串查找的方式,复杂一点可以用正则表达式,但是有时候正则很麻烦的,因为html代码本身就比较麻烦,像常用的img标签,这个东东到了浏览器上就没了闭合标签(一直还没搞懂为什么),想用XML解析,也是同样的原因根本解析不了,今天发现一个解析html控件,用了一下,非常好用。

这个控件叫做Html Agility Pack,主页在这儿:http://htmlagilitypack.codeplex.com/

这儿还有一篇blog介绍怎么使用的 (English):http://olussier.net/2010/03/30/easily-parse-html-documents-in-csharp/

我直接把例子贴这儿,一看就明白。因为是作为xml解析的,所以呢,少不了XPath,如果不懂这个东西的话,赶紧看看吧,现在xpath语法都扩展到css里面了,语法比较简单,先看看基础的就行了。

最基本的使用方法不是SelectSingleNode,而是GetElementById,这是与XmlDocument不同的地方。

// The HtmlWeb class is a utility class to get the HTML over HTTP
HtmlWeb htmlWeb = new HtmlWeb();
 
// Creates an HtmlDocument object from an URL
HtmlAgilityPack.HtmlDocument document = htmlWeb.Load("http://www.somewebsite.com");
 
// Targets a specific node
HtmlNode someNode = document.GetElementbyId("mynode");
 
// If there is no node with that Id, someNode will be null
if (someNode != null)
{
  // Extracts all links within that node
  IEnumerable allLinks = someNode.Descendants("a");
 
  // Outputs the href for external links
  foreach (HtmlNode link in allLinks)
  {
    // Checks whether the link contains an HREF attribute
    if (link.Attributes.Contains("href"))
    {
      // Simple check: if the href begins with "http://", prints it out
      if (link.Attributes["href"].Value.StartsWith("http://"))
        Console.WriteLine(link.Attributes["href"].Value);
    }
  }
}

使用xpath

// Extracts all links under a specific node that have an href that begins with "http://"
HtmlNodeCollection allLinks = document.DocumentNode.SelectNodes("//*[@id='mynode']//a[starts-with(@href,'http://')]");
 
// Outputs the href for external links
foreach (HtmlNode link in allLinks)
    Console.WriteLine(link.Attributes["href"].Value);

One more

xpath = "//table[@id='1' or @id='2' or @id='3']//a[@onmousedown]"; 
xpath = "//ul[@id='wg0']//li[position()<4]/h3/a"; 
xpath = "//div[@class='resitem' and position()<4]/a";
xpath = "//li[@class='result' and position()<4]/a";

 

一句话,非常好用!

posted @ 2012-01-05 18:32 KymoWang 阅读(319) 评论(2) 编辑

以前做页面有时因为层的设计不合理出现一些错误的页面覆盖,下面是从一篇blog中看到的QQ空间层的设计,很受启发,原文好像被删了

posted @ 2011-12-30 16:15 KymoWang 阅读(10) 评论(0) 编辑
IIS7的Forms Authentication and URL Authorization默认是不验证静态内容的访问权限的,这是为了向前兼容,加了一个precondition,从而只验证asp.net的内容。去掉这些precondition的方法是在web.config里面加上下面的配置。
 
 
        
       
        
        
        
        
 
 
posted @ 2011-10-31 18:35 KymoWang 阅读(15) 评论(0) 编辑
摘要: 今天仔细差了一下SQL Sever中各种role的定义,记录备查 Predefined database roles You may need to create your own, but you ...阅读全文
posted @ 2011-07-13 10:11 KymoWang 阅读(88) 评论(0) 编辑
摘要: 今天使用Path.GetTempFileName创建临时文件,抛出了exception:System.IO.IOException: The file exists。看了一下MSDN,The GetTempFileName method will raise an IOException if it is used to create more than 65535 files without deleting previous temporary files.The GetTempFileName method will raise an IOException if no unique t阅读全文
posted @ 2011-07-06 10:33 KymoWang 阅读(79) 评论(0) 编辑
摘要: JavaScript的变量分为全局变量和局部变量全局变量(Global) 全局变量是指在任何地方都可以访问的变量,有两种情况在function外面声明,不论是否用var 关键字在function里面声明,不使用var关键字,当然声明的语句必须被执行才可以 局部变量(Local ) 局部变量只能在被声明的function内部才能访问 在function里面声明,不使用var关键字 两点要注意的地方先看代码 alert(i); //输出undefined for (var i = 0; i < 1; i++){}; alert(i); //输出1JavaScript不存在语句作用域,在语句内阅读全文
posted @ 2011-07-05 10:05 KymoWang 阅读(89) 评论(2) 编辑
摘要: 两个小function实现XML和string相互转化 //convert string to xml object function String2XML(xmlString) { // for IE if (window.ActiveXObject) { var xmlobject = new ActiveXObject("Microsoft.XMLDOM"); xmlobject.async = "false"; xmlobject.loadXML(xmlstring); return xmlobject; } // for other brows阅读全文
posted @ 2011-07-04 14:55 KymoWang 阅读(237) 评论(0) 编辑
摘要: 当我们远程操作win2008的时候,因为server上没有speaker,所以默认是没有办法播放声音的。解决办法:[start] -> [run]: tsconfig.msc选中[RD Session Host Configuration]在中间的[Connections]下面选中正在使用的connection切换到[Client Settings]选项卡反选[Audio and video playback] / [Audio recording] (置于非选中状态)重新remote然后就可以发现音量可以调节大小了。阅读全文
posted @ 2011-05-04 18:57 KymoWang 阅读(30) 评论(0) 编辑
摘要: 自定义config的作用主要是增强config的可读性,容易管理,直接进入正题先来比较一下使用AppSettings: 使用custom section: 下面是创建过程Step1 建立自定义classusing System;using System.Configuration;public class BlogSettings : ConfigurationSection{ private static BlogSettings settings = ConfigurationManager.GetSection("BlogSettings") as BlogSetti阅读全文
posted @ 2011-02-28 16:33 KymoWang 阅读(101) 评论(0) 编辑
摘要: SQL Server 本地时间和UTC时间的相互转换的代码DECLARE @LocalDate DATETIME,@UTCDate DATETIME,@LocalDate2 DATETIMESET @LocalDate = GETDATE()SET @UTCDate = DATEADD(hour, DATEDIFF(hour,GETDATE(),GETUTCDATE()), @LocalDate)SET @LocalDate2 = DATEADD(hour, DATEDIFF(hour,GETUTCDATE(),GETDATE()), @UTCDate)SELECT '1. Now&#阅读全文
posted @ 2011-02-22 17:33 KymoWang 阅读(278) 评论(0) 编辑