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处理
} }

  

转载于:https://www.cnblogs.com/leoxjy/p/9608632.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值