• DotnetSpider爬虫简单示例 net core


    文章地址

    https://blog.csdn.net/sD7O95O/article/details/78097556
    

     

     安装爬虫框架  NUGET 安装DotnetSpider

    创建HTTP协议数据包

                var site = new Site
                {
                    CycleRetryTimes = 1,
                    SleepTime = 200,
                    Headers = new Dictionary<string, string>()
                    {
                        {"Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8" },
                        {"Accept-Encoding","gzip, deflate, br" },
                        {"Accept-Language","zh-CN,zh;q=0.9" },
                        {"X-Requested-With","XMLHttpRequest" },
                        { "Referer", "https://blog.csdn.net/sD7O95O/article/details/78096027"},
                        { "Connection","keep-alive" },
                        { "Content-Type","text/html; charset=UTF-8" },
                        { "Host","blog.csdn.net"},
                        { "User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"}
                    }
                };
    site.AddStartUrl(surl);

    创建一个爬虫

                Spider spider = Spider.Create(site,             // 
                    new QueueDuplicateRemovedScheduler(),
                    new CsdnArticleProcessor()                  //这个自定义的处理器
                ).AddPipeline(new CsdnArticlePipeline());       //自定义管道
    

      

    设置爬虫

            spider.Downloader = new HttpClientDownloader();
                spider.ThreadNum = 1;
                spider.EmptySleepTime = 3000;
    

      

    运行

     spider.Run();
    

      

     

    CsdnArticleProcessor 处理器  xpath 分析就不贴了
    public class CsdnArticleProcessor : BasePageProcessor
        {
            protected override void Handle(Page page)
            {
                //page.Selectable.SelectList(Selectors.XPath("//table[@id='ip_list']/tr[2]/td[2]/text()")).Nodes();
    
                // 利用 Selectable 查询并构造自己想要的数据对象
                //xpath 得到自己需要的数据
           //...
           //page.AddResultItem("CountryResult", results);放入管道
            }
        }
    
        class CsdnArticlePipeline : BasePipeline
        {
    
            public override void Process(IEnumerable<ResultItems> resultItems, ISpider spider)
            {
          var conlist = resukt.GetResultItem("CountryResult");获取数据
    //DB处理
    } }

      

  • 相关阅读:
    锂电池充电!
    触电
    记录一次调试过程中烧毁电脑主板的经历!
    如何计算一个CPU的MIPS
    铜线的载流能力问题。
    用格西烽火串口助手制作程控命令协议!
    从qt编程看内存分区。
    贴片LED用法
    RtlWerpReportException failed with status code :-1073741823. Will try to launch the process directly
    uboot学习——基于S3C2440的u-boot-1.1.6分析(一)
  • 原文地址:https://www.cnblogs.com/leoxjy/p/9608632.html
Copyright © 2020-2023  润新知