scrapy框架之(CrawlSpider)
一.CrawlSpider简介
- 如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法?
- 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法)。
- 方法二:基于CrawlSpider的自动爬取进行实现(更加简洁和高效)。
一.简介
CrawlSpider其实是Spider的一个子类,除了继承到Spider的特性和功能外,还派生除了其自己独有的更加强大的特性和功能。其中最显著的功能就是”LinkExtractors链接提取器“。Spider是所有爬虫的基类,其设计原则只是为了爬取start_url列表中网页,而从爬取到的网页中提取出的url进行继续的爬取工作使用CrawlSpider更合适。
二.使用
1.创建scrapy工程:scrapy startproject projectName
2.创建爬虫文件:scrapy genspider -t crawl spiderName www.xxx.com
--此指令对比以前的指令多了 "-t crawl",表示创建的爬虫文件是基于CrawlSpider这个类的,而不再是Spider这个基类。
3.观察生成的爬虫文件
爬虫文件.py
- # -*- coding: utf-8 -*-
- import scrapy
- from scrapy.linkextractors import LinkExtractor
- from scrapy.spiders import CrawlSpider, Rule
- #不再是引入spider,而是引入了crawlspider,还引入了LinkExtracor(连接提取器),Rule解析器
- class ChoutiSpider(CrawlSpider):
- name = 'chouti'
- #allowed_domains = ['www.xxx.com']
- start_urls = ['https://dig.chouti.com/r/scoff/hot/1']
- #allow后面跟着正则匹配,用正则去匹配符合的连接
#rule规则解析器则会去把提取器提取到的连接发起请求,并把获得的响应对象用回调函数去解析
#follow表示是否把连接解析器继续作用到提取到的url中(是否提取全站的url)
#这是一个元组- rules = (
- Rule(LinkExtractor(allow=r'Items/'), callback='parse_item', follow=True),
- )
- def parse_item(self, response):
- item = {}
- #item['domain_id'] = response.xpath('//input[@id="sid"]/@value').get()
- #item['name'] = response.xpath('//div[@id="name"]').get()
- #item['description'] = response.xpath('//div[@id="description"]').get()
- return item
案例一:(全站提取)
- # -*- coding: utf-8 -*-
- import scrapy
- from scrapy.linkextractors import LinkExtractor
- from scrapy.spiders import CrawlSpider, Rule
- class ChoutiSpider(CrawlSpider):
- name = 'chouti'
- # allowed_domains = ['www.xxx.com']
- start_urls = ['https://dig.chouti.com/r/scoff/hot/1']
- #把这个单独写比较好看
- link=LinkExtractor(allow=r'/r/scoff/hot/\d+')
- rules = (
- Rule(link,callback='parse_item', follow=True),
- )
- def parse_item(self, response):
- print(response)
- #这样就可以迭代提取到我们想要的所有内容,因为其起始页的url为:https://dig.chouti.com/r/scoff/hot/1
案例二:(第一页没有数字编号的)
- class ChoutiSpider(CrawlSpider):
name = 'chouti'
# allowed_domains = ['www.xxx.com']
start_urls = ['https://www.qiushibaike.com/text/']
#把这个单独写比较好看- link=LinkExtractor(allow=r'/text/page/\d+/')
link1=LinkExtractor(allow=r'/text/')
rules = (
Rule(link,callback='parse_item', follow=True),
Rule(link1, callback='parse_item', follow=True),
)- def parse_item(self, response):
print(response)
- #注意观察器其实url:
- https://www.qiushibaike.com/text/
#第一页没有数字表示
案例三:(正匹配会有很多相似的,限定开头或者结尾)
- class ChoutiSpider(CrawlSpider):
- name = 'chouti'
- # allowed_domains = ['www.xxx.com']
- start_urls = ['https://www.qiushibaike.com/pic/']
- # 把这个单独写比较好看
#这边的?记得转义\- link = LinkExtractor(allow=r'/pic/page/\d+\?s=')
- link1 = LinkExtractor(allow=r'/pic/$') #提取第一页这个匹配会有很多其他的干扰,这些并不是我们想要的,要限定结尾$
- rules = (
- Rule(link, callback='parse_item', follow=True),
- Rule(link1, callback='parse_item', follow=True),
- )
- def parse_item(self, response):
- print(response)
注:如果allow没有为空,那就是匹配网页中所有的url
scrapy框架之(CrawlSpider)的更多相关文章
- 爬虫scrapy框架之CrawlSpider
爬虫scrapy框架之CrawlSpider 引入 提问:如果想要通过爬虫程序去爬取全站数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模 ...
- Python网络爬虫之Scrapy框架(CrawlSpider)
目录 Python网络爬虫之Scrapy框架(CrawlSpider) CrawlSpider使用 爬取糗事百科糗图板块的所有页码数据 Python网络爬虫之Scrapy框架(CrawlSpider) ...
- Scrapy框架之CrawlSpider
提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法二:基 ...
- 16.Python网络爬虫之Scrapy框架(CrawlSpider)
引入 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法 ...
- scrapy框架之CrawlSpider操作
提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法二:基 ...
- 爬虫开发11.scrapy框架之CrawlSpider操作
提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法二:基 ...
- scrapy框架基于CrawlSpider的全站数据爬取
引入 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法 ...
- 16,Python网络爬虫之Scrapy框架(CrawlSpider)
今日概要 CrawlSpider简介 CrawlSpider使用 基于CrawlSpider爬虫文件的创建 链接提取器 规则解析器 引入 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话, ...
- python爬虫入门(八)Scrapy框架之CrawlSpider类
CrawlSpider类 通过下面的命令可以快速创建 CrawlSpider模板 的代码: scrapy genspider -t crawl tencent tencent.com CrawSpid ...
随机推荐
- jqgrid 编辑表格(包含下拉框)
.1在jqgrid 按钮 <asp:JQGridColumn TextAlign=" DataField="act" Visible="True" ...
- 复习action委托
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- clojure-emacs-autocomplete
1. https://github.com/clojure-emacs/cider#keyboard-shortcuts 2. install emacs 24.5 3. http://clojure ...
- POJ3259 Wormholes(SPFA判断负环)
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- PopupWindow简单使用(一)
1.构造函数 //方法一: public PopupWindow (Context context) //方法二: public PopupWindow(View conten ...
- springcloud集成swaggerui做动态接口文档
1.配置文件pom,一定要使用2.4以上的,2.4默认请求方式是json,会导致getmapping设置参数类型对对象时,swaggerui界面不能指定为其他类型,反正就是各种坑,不建议用 <d ...
- wp后台更新瓷片
下载源码 还有一种方式,更新瓷片方式 1. /// <summary> /// 定时更新磁贴 /// </summary> public class ShellUpdate { ...
- 解决"要执行请求的操作,WordPress需要访问您网页服务器的权限"
比如我们在VPS主机中创建WordPress站点的时候,会有需要在线安装主题.插件等,但是点击下载安装的时候会有"要执行请求的操作,WordPress需要访问您网页服务器的权限. 请输入您的 ...
- Android学习笔记AutoCompleteTextView的使用
activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...
- EF进阶篇(三)——上下文
前言 上下文,到底什么是上下文,且听我仔细吹来. 内容 在对EF实体进行关系操作的时候,第一步需要我们创建上下文实例对象,然后根据实体的变化进而通过上下文对该实体进行状态的修改,我的理解就是上下文就是 ...