Crawlspider

一:Crawlspider简介

    CrawlSpider其实是Spider的一个子类,除了继承到Spider的特性和功能外,还派生除了其自己独有的更加强大的特性和功能。其中最显著的功能就是”LinkExtractors链接提取器“。Spider是所有爬虫的基类,其设计原则只是为了爬取start_url列表中网页,而从爬取到的网页中提取出的url进行继续的爬取工作使用CrawlSpider更合适。

二:CrawlSpider整体的爬取流程:

  a)爬虫文件首先根据其实url,获取该url的网页内容

  b)链接提取器会根据提取规则将步骤a中网页内容中的链接进行提取

  c)规则解析器会根据指定解析规则将链接提取器中提取到的链接中的网页内容根据指定的规则进行解析

  d)将解析数据封装到item中,然后提交给管道进行持久化存储

三:Crawlspider使用

实例:爬取https://www.qiushibaike.com/主页帖子作者以及内容

1.创建scrapy工程

2.创建爬虫文件

注意:对比以前的指令多了 "-t crawl",表示创建的爬虫文件是基于CrawlSpider这个类的,而不再是Spider这个基类。

3.生成的目录结构如下:

CrawlDemo.py爬虫文件设置:

  LinkExtractor:顾名思义,链接提取器。
  Rule : 规则解析器。根据链接提取器中提取到的链接,根据指定规则提取解析器链接网页中的内容。

  Rule参数介绍:

    参数1:指定链接提取器

    参数2:指定规则解析器解析数据的规则(回调函数)

    参数3:是否将链接提取器继续作用到链接提取器提取出的链接网页中,当callback为None,参数3的默认值为true。

  rules=( ):指定不同规则解析器。一个Rule对象表示一种提取规则。

  1. # -*- coding: utf-8 -*-
  2. import scrapy
  3. from scrapy.linkextractors import LinkExtractor
  4. from scrapy.spiders import CrawlSpider, Rule
  5.  
  6. from crawlPro.items import CrawlproItem
  7. class CrawldemoSpider(CrawlSpider):
  8. name = 'crawlDemo'
  9. # allowed_domains = ['www.qiushibaike.com']
  10. start_urls = ['http://www.qiushibaike.com/']
  11. #rules元祖中存放的是不同规则解析器(封装好了某种解析规则)
  12. rules = (
  13. # Rule: 规则解析器,可以将连接提取器提取到的所有连接表示的页面进行指定规则(有中间的回调函数决定)的解析
  14. #LinkBxtractor:连接提取器,会去上面起始url响应回来的页面中,提取指定的url
  15. Rule(LinkExtractor(allow=r'/8hr/page/\d+'), callback='parse_item', follow=True), #follow=True可以跟进保证将所有页面都提取出来(实际就是去重功能)
  16. )
  17.  
  18. def parse_item(self, response):
  19. # i = {}
  20. # #i['domain_id'] = response.xpath('//input[@id="sid"]/@value').extract()
  21. # #i['name'] = response.xpath('//div[@id="name"]').extract()
  22. # #i['description'] = response.xpath('//div[@id="description"]').extract()
  23. # return i
  24. divs=response.xpath('//div[@id="content-left"]/div')
  25. for div in divs:
  26. item=CrawlproItem()
  27. #提取糗百中段子的作者
  28. item['author'] = div.xpath('./div[@class="author clearfix"]/a[2]/h2/text()').extract_first().strip('\n')
  29. # 提取糗百中段子的内容
  30. item['content'] = div.xpath('.//div[@class="content"]/span/text()').extract_first().strip('\n')
  31.  
  32. yield item #将item提交到管道

item.py文件设置:

  1. # -*- coding: utf-8 -*-
  2.  
  3. # Define here the models for your scraped items
  4. #
  5. # See documentation in:
  6. # https://doc.scrapy.org/en/latest/topics/items.html
  7.  
  8. import scrapy
  9.  
  10. class CrawlproItem(scrapy.Item):
  11. # define the fields for your item here like:
  12. # name = scrapy.Field()
  13. author=scrapy.Field()
  14. content=scrapy.Field()

pipelines.py管道文件设置:

  1. # -*- coding: utf-8 -*-
  2.  
  3. # Define your item pipelines here
  4. #
  5. # Don't forget to add your pipeline to the ITEM_PIPELINES setting
  6. # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
  7.  
  8. class CrawlproPipeline(object):
  9. def __init__(self):
  10. self.fp = None
  11.  
  12. def open_spider(self,spider):
  13. print('开始爬虫')
  14. self.fp = open('./data.txt','w',encoding='utf-8')
  15.  
  16. def process_item(self, item, spider):
  17. # 将爬虫文件提交的item写入文件进行持久化存储
  18. self.fp.write(item['author']+':'+item['content']+'\n')
  19. return item
  20.  
  21. def close_spider(self,spider):
  22. print('结束爬虫')
  23. self.fp.close()

设置代理:

middlewares.py中间件:

设置代理:ip地址可以通过以下几个链接查找

http://ip.seofangfa.com/

settings.py里面设置:

  1. DOWNLOADER_MIDDLEWARES = {
  2. 'crawlPro.middlewares.Mydaili': 543, #Mydaili名字就是中间件里面的类名
  3. }

middlewares.py中间件设置:

  1. class Mydaili(object):
  2. def process_request(self,request,spider):
  3. request.meta['proxy'] = "http://119.28.195.93:8888"

爬虫Scrapy框架-Crawlspider链接提取器与规则解析器的更多相关文章

  1. 网络爬虫之scrapy框架(CrawlSpider)

    一.简介 CrawlSpider其实是Spider的一个子类,除了继承到Spider的特性和功能之外,还派生了其自己独有的更强大的特性和功能.其中最显著的功能就是"LinkExtractor ...

  2. Scrapy 框架 CrawlSpider 全站数据爬取

    CrawlSpider 全站数据爬取 创建 crawlSpider 爬虫文件 scrapy genspider -t crawl chouti www.xxx.com import scrapy fr ...

  3. python爬虫之Scrapy框架(CrawlSpider)

    提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬去进行实现的(Request模块回调) 方法二:基于CrawlSpi ...

  4. 爬虫scrapy框架之CrawlSpider

    爬虫scrapy框架之CrawlSpider   引入 提问:如果想要通过爬虫程序去爬取全站数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模 ...

  5. 全栈爬取-Scrapy框架(CrawlSpider)

    引入 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法 ...

  6. python爬虫scrapy框架

    Scrapy 框架 关注公众号"轻松学编程"了解更多. 一.简介 Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛. 框架的力量 ...

  7. 爬虫Ⅱ:scrapy框架

    爬虫Ⅱ:scrapy框架 step5: Scrapy框架初识 Scrapy框架的使用 pySpider 什么是框架: 就是一个具有很强通用性且集成了很多功能的项目模板(可以被应用在各种需求中) scr ...

  8. Python网络爬虫-Scrapy框架

    一.简介 Spider是所有爬虫的基类,其设计原则只是为了爬取start_url列表中网页,而从爬取到的网页中提取出的url进行继续的爬取工作使用CrawlSpider更合适. 二.使用 1.创建sc ...

  9. python爬虫scrapy框架——人工识别登录知乎倒立文字验证码和数字英文验证码(2)

    操作环境:python3 在上一文中python爬虫scrapy框架--人工识别知乎登录知乎倒立文字验证码和数字英文验证码(1)我们已经介绍了用Requests库来登录知乎,本文如果看不懂可以先看之前 ...

随机推荐

  1. (转)SQL注入攻击简介

    如果你是做Javaweb应用开发的,那么必须熟悉那声名狼藉的SQL注入式攻击.去年Sony就遭受了SQL注入攻击,被盗用了一些Sony play station(PS机)用户的数据.在SQL注入攻击里 ...

  2. linux python升级及全局环境变量设置

    1.下载pythonwget https://www.python.org/ftp/python/3.4.5/Python-3.4.5.tgz 或者去官网下载压缩包 2.安装python3依赖yum ...

  3. 【转】iOS-生成Bundle包-引入bundle-使用bundle

    在我们使用第三方框架时,常常看到XXX.bundle的文件. 我们找到该文件,显示包内容,大致看到很多资源文件:图片.配置文本.XIB文件……   什么是Bundle文件? 简单理解,就是资源文件包. ...

  4. IE下contentWindow对象与FF、Chrome下的区别

    在ie中frame(iframe)标签通过name和id获取的对象是不同的. 通过name获取的本身就是contentWindow对象.所以 在ie中不用再找contentWindow了 例: let ...

  5. OC和C++的混用1

    //Objective-C类 /*在混用之前需要做一步非常重要的事:不是代码而是编译器选项,在做混合编译之前一定要把编译器的Compile Sources As选项改为Objective C++. 修 ...

  6. xmpp 协议详解

    XMPP(可扩展消息处理现场协议)是基于可扩展标记语言(XML)的协议,它用于即时消息(IM)以及在线现场探测.它在促进服务器之间的准即时操作.这个协议可能最终允许因特网用户向因特网上的其他任何人发送 ...

  7. virtualvenv+django+uWSGI+nginx 部署

    原创博文 转载请注明出处! 1. virtualvenv 2. django 3. uWSGI 4. nginx 5. 踩坑记录 1. virtualvenv virtualvenv install ...

  8. 浅谈JavaScript中的正则表达式(适用初学者观看)

    浅谈JavaScript中的正则表达式 1.什么是正则表达式(RegExp)? 官方定义: 正则表达式是一种特殊的字符串模式,用于匹配一组字符串,就好比用模具做产品,而正则就是这个模具,定义一种规则去 ...

  9. 35. Search Insert Position@python

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  10. 【转】再谈 最速下降法/梯度法/Steepest Descent

    转载请注明出处:http://www.codelast.com/ 最速下降法(又称梯度法,或Steepest Descent),是无约束最优化领域中最简单的算法,单独就这种算法来看,属于早就“过时”了 ...