c#html Analytical Weapon Htmlagilitypack

Source: Internet
Author: User
Tags xpath

Htmlagilitypack is an open-source parsing of HTML elements of the class library, the biggest feature is that you can use XPath to parse HMTL, if you have previously worked with C # XML, then using the Htmlagilitypack will be handy. Currently the latest version is 1.4.6, as follows: http://htmlagilitypack.codeplex.com/the current stable version is 1.4.6, last update or 2012, so it is stable, basic functions, and there is no need to update.

When it comes to htmlagilitypack, it is necessary to introduce an auxiliary tool that does not know how the page structure is analyzed when others are using it. Anyway, I'm using a tool called Hapexplorer, which is officially provided. Very useful. We'll show you how to use it when we use it.

Or take Liaoning province as an example: http://www.tianqihoubao.com/lishi/ln.htm, open the page, right-click on the page source code, paste into the hapexplorer, you can also open the link directly in Hapexplorer, As the following animation shows:

We can see that the XPath address on the right, the end of the Div, the following is the DL tag, which is the line we are going to collect. Below we use code to get the above structure. First look at the code to get the page source code:

12345678910 publicstaticstringGetWebClient(stringurl){    stringstrHTML = "";    WebClient myWebClient = new WebClient();                Stream myStream = myWebClient.OpenRead(url);    StreamReader sr = newStreamReader(myStream, Encoding.Default);//注意编码    strHTML = sr.ReadToEnd();    myStream.Close();    returnstrHTML;}

public static void ParsePageByArea(String cityCode) {      //更加链接格式和省份代码构造URL      String url = String.Format( "http://www.tianqihoubao.com/lishi/{0}.htm" , cityCode);      //下载网页源代码       var docText = HtmlHelper.GetWebClient(url);      //加载源代码,获取文档对象      var doc =  new HtmlDocument(); doc.LoadHtml(docText);      //更加xpath获取总的对象,如果不为空,就继续选择dl标签      var res = doc.DocumentNode.SelectSingleNode( @"/html[1]/body[1]/div[1]/div[6]/div[1]/div[1]/div[3]" );      if (res !=  null )      {          var list = res.SelectNodes( @"dl" ); //选择标签数组          if (list.Count < 1)  return ;          foreach ( var item  in list)          {              var dd = item.SelectSingleNode( @"dd" ).SelectNodes( "a" );              foreach ( var node  in dd)              {                  var text = node.InnerText.Trim();                  //拼音代码要从href属性中进行分割提取                  var herf = node.Attributes[ "href" ].Value.Trim().Split( ‘/‘ ‘.‘ );                  Console.WriteLine( "{0}:{1}" , text, herf[herf.Length - 2]);              }          }      } }Source Address: http://www.cnblogs.com/asxinyu/p/CSharp_HtmlAgilityPack_XPath_Weather_Data.html

c#html Analytical Weapon Htmlagilitypack

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.