zoukankan      html  css  js  c++  java
  • HtmlAgilityPack 解析HTMl 真不错

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using HtmlAgilityPack;




    namespace DemoCnBlogs
    {
        class Program
        {
            static void Main(string[] args)
            {
                HtmlWeb web = new HtmlWeb();
                HtmlDocument doc = web.Load("http://www.cnblogs.com/pick/");


                HtmlNode node = doc.GetElementbyId("post_list");


                StreamWriter sw = File.CreateText("log.txt");


                foreach (HtmlNode child in node.ChildNodes)
                {
                    if (child.Attributes["class"] == null || child.Attributes["class"].Value != "post_item")
                        continue;
                    HtmlNode hn = HtmlNode.CreateNode(child.OuterHtml);


                    ///如果用child.SelectSingleNode("//*[@class=\"titlelnk\"]").InnerText这样的方式查询,是永远以整个document为基准来查询,
                    ///这点就不好,理应以当前child节点的html为基准才对。


                    Write(sw, String.Format("推荐:{0}", hn.SelectSingleNode("//*[@class=\"diggnum\"]").InnerText));
                    Write(sw, String.Format("标题:{0}", hn.SelectSingleNode("//*[@class=\"titlelnk\"]").InnerText));
                    Write(sw, String.Format("介绍:{0}", hn.SelectSingleNode("//*[@class=\"post_item_summary\"]").InnerText));
                    Write(sw, String.Format("信息:{0}", hn.SelectSingleNode("//*[@class=\"post_item_foot\"]").InnerText));


                    Write(sw, "----------------------------------------");


                }


                sw.Close();


                Console.ReadLine();
            }


            static void Write(StreamWriter writer, string str)
            {
                Console.WriteLine(str);
                writer.WriteLine(str);
            }




        }
    }
  • 相关阅读:
    干货!常见的SQL面试题:经典50例!
    图解SQL基础知识,小白也能看懂的SQL文章!
    30多个Java实战项目,全部在这里了。
    Spring的@PropertySource注解使用
    20201226[java]两数相加
    1013.表-约束-唯一、主键、外键和排他
    1012.表-约束-检查
    1011.表-生成列
    1010.表默认值
    1009.表基础-初识表
  • 原文地址:https://www.cnblogs.com/eaglezzb/p/4176511.html
Copyright © 2011-2022 走看看