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 ...
随机推荐
- tensor 维度 问题。
tf.argmax takes two arguments: input and dimension. example: tf.argmx(arr, dimension = 1). or tf.arg ...
- SVN常见问题及解决方式(二)
1.分支不同 ==> update merge(svn自动合并)2.分支冲突 ==> 协商解决冲突,选择一个正确的版本覆盖(最新的正确直接Revert最新):出现四个文件.黄色感叹号代表S ...
- <abbr> 元素的样式为显示在文本底部的一条虚线边框,当鼠标悬停在上面时会显示完整的文本(只要您为 <abbr> title 属性添加了文本)
<abbr title="World Wide Web">WWW</abbr><br><abbr title="Real Sim ...
- Android onKeyDown、onKeyUp、dispatchKeyEvent的区别
1. onKeyDown.onKeyUp.dispatchKeyEvent的区别和使用场景 区别: 1.1 onKeyDown.onKeyUp是按键事件的回调接口(冒泡式调用),dispatchKey ...
- psimpl_v7_win32_demo
psimpl - generic n-dimensional polyline simplification 通用N维折线简化程序 Author - Elmar de Koning 作者 - Elma ...
- (11)Web程序保存状态的几种方式,Application,Session,Cookie,ViewState
WEb程序保存状态的方式有这样几种: 1.Application:保存在Application中的数据是全局有效的:Application里面存放的应该是访问多修 改较少并且是全局至少大部分 ...
- poj3080 Blue Jeans(暴枚+kmp)
Description The Genographic Project is a research partnership between IBM and The National Geographi ...
- .net 基元类型,引用类型和值类型
基元类型(primitive type): 编译器直接支持的数据类型称为基元类型(primitive type). string 与 String: 由于C#中的string (一个关键字)直接映射到 ...
- 初学reactNative遇到的问题总结
1.undefined is not an object (evaluating '_react3.default.PropTypes.shape') Navigator已经不再react nativ ...
- C# System.Threading.Timer 定时器
前提: 需要引入 System.Threading: 描述: 在很多时间我们都需要进行延迟执行,或是定时执行一些指定业务,这个时候使用 Timer 是最合适的,而且 Timer 是Cpu 级别处理对 ...