zoukankan      html  css  js  c++  java
  • C#开源爬虫NCrawler源代码解读以及将其移植到python3.2(2)

    在上一篇中,我们提到了管道这个概念(pipeline),其实所有的管道都实现了同一接口叫

    	public interface IPipelineStep
    	{
    		void Process(Crawler crawler, PropertyBag propertyBag);
    	}

    所有爬到的网址都将被 构造 Crawler 时通过构造函数注入的管道 处理。

    一般来说第一个处理的管道是 HtmlDocumentProcessor,它负责解析网页。那么其实现接口的具体函数就很值得一看。

    在函数的开始处NCrawler使用了AOP技术做了一次参数的非空检查,使用的AOP框架是轻量级的,叫 AspectF

    			AspectF.Define.
    				NotNull(crawler, "crawler").
    				NotNull(propertyBag, "propertyBag");

    紧接着函数进行了一系列操作,把HTML的文本,包括 title , meta 提取出来,找出其中 links ,然后开启循环针对里面每个 link 整形重新添加到 爬虫的 等待爬行的URL的序列,代码如下:

    			foreach (string link in links.Links.Union(links.References))
    			{
    				if (link.IsNullOrEmpty())
    				{
    					continue;
    				}
    
    				string decodedLink = ExtendedHtmlUtility.HtmlEntityDecode(link);
    				string normalizedLink = NormalizeLink(baseUrl, decodedLink);
    				if (normalizedLink.IsNullOrEmpty())
    				{
    					continue;
    				}
    
    				crawler.AddStep(new Uri(normalizedLink), propertyBag.Step.Depth + 1,
    					propertyBag.Step, new Dictionary<string, object>
    						{
    							{Resources.PropertyBagKeyOriginalUrl, link},
    							{Resources.PropertyBagKeyOriginalReferrerUrl, propertyBag.ResponseUri}
    						});
    			}


  • 相关阅读:
    Linux命令: ls -l显示文件和目录的详细资料
    Linux命令: ls -F
    Linux命令: pwd显示工作路径
    Linux命令: cd /home 进入'/home'目录
    Linux命令: cd ../.. 返回上两级目录
    Linux命令: cd
    boost::mpl::eval_if的使用方法
    【block第四篇】实现
    Android中pendingIntent的深入理解
    hdu 1565 方格取数(1)(状态压缩dp)
  • 原文地址:https://www.cnblogs.com/rav009/p/5131145.html
Copyright © 2011-2022 走看看