分布式下的爬虫Scrapy应该如何做-规则自动爬取及命令行下传参

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

本次探讨的主题是规则爬取的实现及命令行下的自定义参数的传递,规则下的爬虫在我看来才是真正意义上的爬虫。

我们选从逻辑上来看,这种爬虫是如何工作的:

我们给定一个起点的url link ,进入页面之后提取所有的ur 链接,我们定义一个规则,根据规则(用正则表达式来限制)来提取我们想要的连接形式,然后爬取这些页面,进行一步的处理(数据提取或者其它动作),然后循环上述操作,直到停止,这个时候有一个潜在的问题,就是重复爬取,在scrapy 的框架下已经着手处理了这些问题,一般来说,对于爬取过滤的问题,通用的处理方式是建立一个地址表,在爬取之前查一下这个地址表,是否已经爬取过,如果是,则直接过滤掉。另一种就是使用现成的通用解决方案,bloom filter

本次讨论的是如何使用CrawlSpider 来进行爬取豆瓣标签下的所有小组的信息:

一,我们新建立一个类,继承自CrawlSpider

fromscrapy.spidersimportCrawlSpider, Rulefromscrapy.linkextractorsimportLinkExtractorfromdouban.itemsimportGroupInfoclassMySpider(CrawlSpider):

关于CrawlSpider的更多说明,请参考:http://doc.scrapy.org/en/latest/topics/spiders.html#crawlspider

二,为了完成命令行下的参数传递,我们需要在类的构造函数里面输入我们想要的参数

:

在命令行下这样使用:

scrapy crawl douban.xp --logfile=test.log -a target=%E6%96%87%E5%85%B7

这样就可以将自定义的参数传入到里面

这里特别说明最后的一行:super(MySpider, self).__init__()

我们转到定义,查看CrawlSpider 的定义:

构造函数会调用私有方法编译rules变量,如果在我们自己定义的Spider里面没有调用方法,会直接报错的。

三,编写规则:

self.rules =(

Rule(LinkExtractor(allow=('/group/explore[?]start=.*?[&]tag=.*?$', ), restrict_xpaths=('//span[@class="next"]')), callback='parse_next_page',follow=True),

)

allow 定义想要提取标签样式,使用正则匹配,restrict_xpaths 严格限制这种标签的范围在指定的标签内,callback ,提取到之后的回调函数。

四,全部代码参考:

fromscrapy.spidersimportCrawlSpider, Rulefromscrapy.linkextractorsimportLinkExtractorfromdouban.itemsimportGroupInfoclassMySpider(CrawlSpider):

name='douban.xp'current=''allowed_domains= ['douban.com']def__init__(self, target=None):ifself.currentisnot'':

target=self.currentiftargetisnotNone:

self.current=target

self.start_urls=['http://www.douban.com/group/explore?tag=%s'%(target)

]

self.rules=(

Rule(LinkExtractor(allow=('/group/explore[?]start=.*?[&]tag=.*?$', ), restrict_xpaths=('//span[@class="next"]')), callback='parse_next_page',follow=True),

)#call the father base functionsuper(MySpider, self).__init__()defparse_next_page(self, response):

self.logger.info(msg='begin init the page %s'%response.url)

list_item= response.xpath('//a[@class="nbg"]')#check the group is not nulliflist_itemisNone:

self.logger.info(msg='cant select anything in selector')returnfora_iteminlist_item:

item=GroupInfo()

item['group_url'] =''.join(a_item.xpath('@href').extract())

item['group_tag'] =self.current

item['group_name'] =''.join(a_item.xpath('@title').extract())yielditemdefparse_start_url(self, response):

self.logger.info(msg='begin init the start page %s'%response.url)

list_item= response.xpath('//a[@class="nbg"]')#check the group is not nulliflist_itemisNone:

self.logger.info(msg='cant select anything in selector')returnfora_iteminlist_item:

item=GroupInfo()

item['group_url'] =''.join(a_item.xpath('@href').extract())

item['group_tag'] =self.current

item['group_name'] =''.join(a_item.xpath('@title').extract())yielditemdefparse_next_page_people(self, response):

self.logger.info('Hi, this is an the next page! %s', response.url)

五,实际运行:

scrapy crawl douban.xp --logfile=test.log -a target=%E6%96%87%E5%85%B7

实际的数据效果:

本次主要解决两个问题:

1.如何从命令行下传递参考

2.如何编写CrawlSpider

里面的演示的功能都比较有限,实际的运行中其实是需要进一步编写其它的规则,比如如何防止被ban,下一篇在简短的介绍下

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航