引言

本篇介绍Crawlspider,相比于Spider,Crawlspider更适用于批量爬取网页

Crawlspider

  • Crawlspider适用于对网站爬取批量网页,相对比Spider类,CrawSpider主要使用规则(rules)来提取链接,通过定义一组规则为跟踪链接提供了遍历的机制。
  • Crawlspider 的强大体现在自动爬取页面所有符合规则的链接并深入下去!

全站数据爬取

编码流程

  1. 新建一个工程

  2. cd 工程

  3. 创建爬虫文件: scrapy genspider -t crawl spiderName 地址 (crawlspider 继承 scrapy.Spider)

    • 链接提取器 LinkExtractor

      • 可以根据指定的规则对指定的链接进行提取

        • 提取的规则就是构造方法中的 allow(‘正则表达式’) 参数决定
    • 规则解析器 Rule
      • 可以将链接提取器提到的链接进行请求,可以根据指定的规则(callback)对请求到的数据进行解析

    Rule(link, callback='parse_item', follow=True)

    • follow = True 表示每一个页码都当作起始url 然后进行链接提取,如果为 false 只能提取到第一页的几个页码。

    • follow = True; 将链接提取器 继续作用到 连接提取器提取到的链接 所对应的 页码源码中。

scrapy中发送请求的几种方式:

  • start_url
  • self.Request()
  • 链接提取器

例子

使用CrawlSpider模板批量爬取(阳光热线问政平台的投诉帖子)的主题、状态和详细内容

地址为:http://wz.sun0769.com/html/top/reply.shtml

① 定义spider

scrapy genspider -t crawl sun 创建一个spider

在该spider文件中:

  • 定义 LinkExtractor 获取每个页面中的页码的url地址。
  • 定义 Rule ,放入 LinkExtractor 以及 callback ,对于 follow 值得话,如果为True得话,将继续作用到 LinkExtractor 提取到的链接 所对应的 页码源码中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# sun.py
# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from sunPro.items import Item1, Item2 class SunSpider(CrawlSpider):
name = 'sun'
# allowed_domains = ['www.xxx.com']
start_urls = ['http://wz.sun0769.com/html/top/reply.shtml']
# 链接提取器(如:获得每一个页码)
link = LinkExtractor(allow=r'page=\d+') # 空的话取所有url
link_1 = LinkExtractor(allow=r'page=$') # 拿到第一页数据
link_detail = LinkExtractor(allow=r'question/\d+/\d+\.shtml') # 拿到第一页数据 . 需要转义 rules = (
# 实例化一个Rule(规则解析器)对象
Rule(link, callback='parse_item', follow=True),
Rule(link_1, callback='parse_item'),
Rule(link_detail, callback='parse_detail'),
# follow = True; 将链接提取器 继续作用到 连接提取器提取到的链接 所对应的 页码源码中
) # 数据解析: 用来解析连接提取器提取到的链接所对应的页码
def parse_item(self, response):
# tr_list = response.xpath('/html/body/div[8]/table[2]/tbody/tr') # xpath中不能含有tbody
tr_list = response.xpath('/html/body/div[8]/table[2]//tr') for tr in tr_list:
title = tr.xpath('./td[3]/a[1]/text()').extract_first()
status = tr.xpath('./td[4]/span/text()').extract_first()
num = tr.xpath('./td[1]/text()').extract_first()
# print(num, title,status)
item = Item2()
item['title'] = title
item['status'] = status
item['num'] = num
yield item
# print(response) # 解析详情页中的新闻内容
def parse_detail(self, response):
content = response.xpath('/html/body/div[9]/table[2]//tr[1]/td//text()').extract()
if content:
content = ''.join(content)
num = response.xpath('/html/body/div[9]/table[1]//tr/td[2]/span[2]').extract_first().split(':')[-1].replace(
r'</span>', '')
# print(num, content)
item = Item1()
item['content'] = content
item['num'] = num
yield item

② 定义Item类

  1. 两个Rule是为了拿到所有页码的url,它对应着 Item2
  2. 另一个Rule是为了拿到所有详情页的url,它对应着 Item1
1
2
3
4
5
6
7
8
9
10
11
12
13
# items.py
import scrapy class Item1(scrapy.Item):
# define the fields for your item here like:
content = scrapy.Field()
num = scrapy.Field() class Item2(scrapy.Item):
# define the fields for your item here like:
title = scrapy.Field()
status = scrapy.Field()
num = scrapy.Field()

③ 定义pipeline

定义pipeline做持久化存储!

  1. open_spider 中开起连接
  2. close_spider 中关闭连接
  3. process_item 中执行数据库得插入操作。

遇到的问题:

  • 第一个问题:主题和状态与详细内容如何对应起来呢?

    • 通过对页面进行分析发现,使用在这两个页面中都有编号,所以增加一个新的字段变化,来连接这两块。
  • 两个数据解析函数不同于使用scrapy.Request进行手动传参,然后通过回调来进行连接。而现在只能定义两个 Item ,在管道中如何判断item类型:

    • item.__class__.__name__ 表示类名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# pipeline.py
import pymysql class SunproPipeline(object):
conn = None
cursor = None def open_spider(self, spider):
self.conn = pymysql.Connection(host='127.0.0.1', user='root', password="2296",
database='spider', charset='utf8') def process_item(self, item, spider):
self.cursor = self.conn.cursor()
if item.__class__.__name__ == 'Item1':
content = item['content']
num = item['num']
query_sql = 'select * from sun where num = %s'
self.cursor.execute(query_sql, (num,))
ret = self.cursor.fetchall()
if ret:
insert_sql = f'update sun set content = %s where num=%s'
try:
self.cursor.execute(insert_sql, (content, num))
self.conn.commit()
except Exception as e:
print(e)
self.conn.rollback()
else:
insert_sql = f'insert into sun(num,content) values (%s, %s)'
try:
self.cursor.execute(insert_sql, (num, content))
self.conn.commit()
except Exception as e:
print(e)
self.conn.rollback()
else:
title = item['title']
status = item['status']
num = item['num']
query_sql = f'select * from sun where num = %s'
self.cursor.execute(query_sql, (num,))
ret = self.cursor.fetchall()
if ret:
insert_sql = f'update sun set title = %s,status = %s where num=%s'
try:
self.cursor.execute(insert_sql, (title, status, num))
self.conn.commit()
except Exception as e:
print(e)
self.conn.rollback()
else:
insert_sql = f'insert into sun(num,title,status) values (%s, %s,%s)'
try:
self.cursor.execute(insert_sql, (num, title, status))
self.conn.commit()
except Exception as e:
print(e)
self.conn.rollback() return item def close_spider(self, spider):
self.cursor.close()
self.conn.close()

Scrapy框架——使用CrawlSpider爬取数据的更多相关文章

  1. 基于scrapy框架输入关键字爬取有关贴吧帖子

    基于scrapy框架输入关键字爬取有关贴吧帖子 站点分析 首先进入一个贴吧,要想达到输入关键词爬取爬取指定贴吧,必然需要利用搜索引擎 点进看到有四种搜索方式,分别试一次,观察url变化 我们得知: 搜 ...

  2. scrapy框架基于CrawlSpider的全站数据爬取

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

  3. Python+Scrapy+Crawlspider 爬取数据且存入MySQL数据库

    1.Scrapy使用流程 1-1.使用Terminal终端创建工程,输入指令:scrapy startproject ProName 1-2.进入工程目录:cd ProName 1-3.创建爬虫文件( ...

  4. 一个scrapy框架的爬虫(爬取京东图书)

    我们的这个爬虫设计来爬取京东图书(jd.com). scrapy框架相信大家比较了解了.里面有很多复杂的机制,超出本文的范围. 1.爬虫spider tips: 1.xpath的语法比较坑,但是你可以 ...

  5. scrapy框架综合运用 爬取天气预报 + 定时任务

    爬取目标网站: http://www.weather.com.cn/ 具体区域天气地址: http://www.weather.com.cn/weather1d/101280601.shtm(深圳) ...

  6. Scrapy 框架 使用 selenium 爬取动态加载内容

    使用 selenium 爬取动态加载内容 开启中间件 DOWNLOADER_MIDDLEWARES = { 'wangyiPro.middlewares.WangyiproDownloaderMidd ...

  7. 信息技术手册可视化进度报告 基于BeautifulSoup框架的python3爬取数据并连接保存到MySQL数据库

    老师给我们提供了一个word文档,里面是一份信息行业热词解释手册,要求我们把里面的文字存进数据库里面,然后在前台展示出来. 首先面临的问题是怎么把数据导进MySQL数据库,大家都有自己的方法,我采用了 ...

  8. Scrapy持久化存储-爬取数据转义

    Scrapy持久化存储 爬虫爬取数据转义问题 使用这种格式,会自动帮我们转义 'insert into wen values(%s,%s)',(item['title'],item['content' ...

  9. Python使用Scrapy框架爬取数据存入CSV文件(Python爬虫实战4)

    1. Scrapy框架 Scrapy是python下实现爬虫功能的框架,能够将数据解析.数据处理.数据存储合为一体功能的爬虫框架. 2. Scrapy安装 1. 安装依赖包 yum install g ...

随机推荐

  1. VSCode 快捷键定义

    默认的 Toggle explore side bar 快捷键为 Ctrl + B, 但是这和 Vim 的快捷键冲突,解决方法: File  >  Preferences  >  Keyb ...

  2. Java&Selenium处理页面Table以及Table中随机位置的数据

    一.摘要 前一段时间公司小伙伴刚刚接触自动化,遇到的一个问题,页面新创建的数据保存后,出现在table中的某个位置,并不一定是第一行还是第几行,这种情况下如何去操控它 本篇博文将介绍处理这个问题的一种 ...

  3. SpiderMan成长记(爬虫之路)

    第一章 爬虫基础 1.1 爬虫基本原理 1.2 请求库 -- urllib库的使用 1.3 请求库 -- requests库的使用 1.4 数据解析 -- 正则基础 1.5 数据解析 -- lxml与 ...

  4. 从Excel中读取数据并批量写入MySQL数据库(基于pymysql)

    一.Excel内容时这样的: 二.最初的代码是这样的: # -*- coding:utf-8 -*-import pymysqlfrom xlrd import open_workbook class ...

  5. CSS3 -- 弹性盒

    新版弹性盒 兼容到IE10及以上 flex小游戏 display: flex; 设置为弹性盒(父元素添加) flex-direction 用来来确定主轴的方向,从而确定基本的项目排列方向. 参数 说明 ...

  6. 无法安装 Composer 怎么办?

    详细方法参见 http://pkg.phpcomposer.com/ 下载 composer.phar ,放置到项目中 在同目录中执行命令行 echo @php "%~dp0composer ...

  7. IntelliJ 如何显示代码的代码 docs

    希望能够在 IntelliJ 代码上面显示方法的 docs. 如何进行显示? 你可以使用 Ctrl + Q 这个快捷键来查看方法的 Docs. https://blog.ossez.com/archi ...

  8. 【集训队作业2018】count

    CSP后第一发Blog. 这道题没有一下子过掉,开始还推出了错的结论.在错误的生成函数里绕了好久-- 很显然的转笛卡尔树,一个笛卡尔树对应一种序列.只要考虑一个笛卡尔树是否合法. 贪心地填数发现,从根 ...

  9. Django基础之request对象

    当一个页面被请求时,django就会创建一个包含本次请求原信息的HttpRequest对象. django会将这个对象自动传递给响应的视图函数,一般视图函数约定俗成地使用request参数承接这个对象 ...

  10. 小程序和vue的区别

    最近开发了一个比较完整的小程序项目,打算总结一下,小程序开发和vue开发的代码上的区别 1.小程序的路由写在app.json文件里,vue写在route.js里 2.小程序用 src="{{ ...