htmlagilitypackDemo

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

namespace htmlagilitypackDemo
{
    class Program
    {
        private const string CategoryListXPath = "./div[2]/div"; //关键点,不同网站分析不同的路径
        private const string CategoryNameXPath = "./table[1]/tr[1]/td[2]/h3[1]"; //关键点,不同网站分析不同的路径
        private const string ChooseXPath = "./a[1]";
        static void Main(string[] args)
        {
            Demo_1();
            Console.Read();
        }
        static void Demo_1()
        {
            Uri url = new Uri("http://news.sogou.com/news?query=%BA%A3%B6%FB&p=42230305&dp=1&_ast=1353663787&_asf=news.sogou.com&time=0&w=03009900&sort=1&mode=1&manual=");
            Uri uriCategory = null;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            WebResponse response = request.GetResponse();

            Stream stream = response.GetResponseStream();
            StreamReader read = new StreamReader(stream, Encoding.GetEncoding("gb2312"));
            string str = read.ReadToEnd();

            HtmlDocument html = new HtmlDocument();
            html.LoadHtml(str);
            //HtmlNode rootNode = html.DocumentNode;
            HtmlNode rootNode = html.GetElementbyId("main");
            HtmlNodeCollection categoryNodeList = rootNode.SelectNodes(CategoryListXPath);
            HtmlNode temp = null;
            var list = new List<Category>();
            foreach (HtmlNode categoryNode in categoryNodeList)
            {
                temp = HtmlNode.CreateNode(categoryNode.OuterHtml);
                HtmlNode singleNode = temp.SelectSingleNode(CategoryNameXPath);
                if (singleNode == null)
                    continue;
                HtmlNodeCollection singleList = temp.SelectNodes(CategoryNameXPath);
                foreach (HtmlNode node in singleList)
                {
                    HtmlNode createNode = HtmlNode.CreateNode(node.OuterHtml);
                    HtmlNode reNode = createNode.SelectSingleNode(ChooseXPath);
                    if (reNode == null)
                        continue;
                    Category category = new Category();
                    category.Subject = reNode.InnerText;
                    Uri.TryCreate(url, reNode.Attributes["href"].Value, out uriCategory);
                    category.IndexUrl = uriCategory.ToString();
                    list.Add(category);
                    Console.WriteLine("name:{0}",category.Subject);
                    Console.WriteLine("url:{0}",category.IndexUrl);
                    //break;
                }
            }
        }
    }
    public class Category
    {
        public string Subject { get; set; }
        public string IndexUrl { get; set; }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值