一、简介

Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,非常出名,非常强悍。所谓的框架就是一个已经被集成了各种功能(高性能异步下载,队列,分布式,解析,持久化等)的具有很强通用性的项目模板。对于框架的学习,重点是要学习其框架的特性、各个功能的用法即可。

二、下载安装

  • Linux:

    • pip install scrapy
  • widows:

三.简单使用

  1.创建scrapy工程:scrapy startproject projectName      (projectName    项目名)

  2.创建爬虫文件:scrapy genspider  spiderName www.xxx.com    (爬虫文件的名称, 起始的url)

  3.编写爬虫文件:在步骤2执行完毕后,会在项目的spiders中生成一个应用名的py爬虫文件,文件源码如下:

# -*- coding: utf-8 -*-
import scrapy class FistSpider(scrapy.Spider):
# 爬虫文件的名称
name = 'fist' # 允许的域名(只有被允许的域名才能发送),一般不用,注掉都可以被发送
# allowed_domains = ['www.xxx.com'] # 起始url列表(只能放url)
start_urls = ['https://baidu.com/'] # 用来实现数据解析
def parse(self, response):
print("响应对象为:",response)

4.设置修改settings.py配置文件相关配置: 

# 修改内容及其结果如下:
19行:USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36' #伪装请求载体身份 22行:ROBOTSTXT_OBEY = False #可以忽略或者不遵守robots协议

5. 运行爬虫文件

    scrapy crawl 爬虫文件名

    scrapy crawl 爬虫文件名 --nolog # 不打印日志运行

持久化存储

存储爬取到的糗事百科热点的内容与发布作者

1、基于终端指令的持久化存储:只可以将parse返回值进行本地文件的存储

class TwoSpider(scrapy.Spider):
name = 'qiubai'
# allowed_domains = ['www.xxx.com'] # 允许的域名
start_urls = ['https://www.qiushibaike.com/text/'] # 起始url列表(只能放url) # 1.基于终端指令的持久化存储:只可以将parse返回值进行本地文件的存储.
def parse(self, response):
div_list = response.xpath('//div[@id="content-left"]/div')
all_data_list = []
for div in div_list:
# author = div.xpath('./div[1]/a[2]/h2/text()')[0].extract()
# 如果可以保证xpath返回的列表中只有一个元素,则可以直接使用下述方法
author = div.xpath('./div[1]/a[2]/h2/text()').extract_first()
content = div.xpath('./a/div/span//text()').extract() # 将列表中所有的selector对象中的数据取出返回也是列表
content = ''.join(content)
dic = {
'author': author,
'content':content
}
all_data_list.append(dic) return all_data_list

scrapy crawl qiubai -o qiubai.csv   # 运行qiubai爬虫文件并将返回内容存到qiubai.csv中

2.基于管道的持久化存储(通用性较强)

  • 1.进行数据解析
  • 2.在item类中定义相关的属性(为了存储解析到的数据)
  • 3.在parse方法中实例化一个item类型的对象
  • 4.将解析到的数据存储到item类型的对象中
  • 5.使用yiled item 将item对象提交给管道 (process_item方法,提交一次调用一次)
  • 6.在管道文件的process_item方法中接收item且对item中存储数据进行持久化存储
  • 7.在settings配置文件中开启管道
    def parse(self, response):
div_list = response.xpath('//div[@id="content-left"]/div')
all_data_list = []
for div in div_list:
# author = div.xpath('./div[1]/a[2]/h2/text()')[0].extract()
# 如果可以保证xpath返回的列表中只有一个元素,则可以直接使用下述方法
author = div.xpath('./div[1]/a[2]/h2/text()').extract_first()
content = div.xpath('./a/div/span//text()').extract() # 将列表中所有的selector对象中的数据取出返回也是列表
content = ''.join(content)
# 实例化一个item类型的对象:用来存储解析到的数据
item = FirstbloodItem()
item["author"] = author
item["content"] = content # 向管道提交item类型的对象
yield item

在item类中定义相关的属性  items.py

import scrapy

class FirstbloodItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
# 定义属性
author = scrapy.Field()
content = scrapy.Field()

在管道文件的process_item方法中接收item且对item中存储数据进行持久化存储  pipeline.py :

可以存到磁盘,数据库中

import pymysql
from redis import Redis class FirstbloodPipeline(object):
fp = None # 该方法只会被执行一次,
def open_spider(self, spider):
self.fp = open('./qiubai.txt', 'w', encoding='utf-8') # 用于处理item的方法,爬虫文件每次提交一次item方法就会被调用一次
def process_item(self, item, spider):
# 取出item中的值
author = item["author"]
content = item["content"]
self.fp.write(author + ":" + content + "\n")
return item #返回给了下一个即将被执行的管道类 # 关闭文件夹
def close_spider(self,spider):
self.fp.close() class MysqlPileLine(object):
# 连接
conn = None
cusor = None
def open_spider(self, spider):
# 连接数据库
self.conn = pymysql.Connect(host='127.0.0.1', port=3306, user='root', password="", db='spider')
print(self.conn) def process_item(self, item, spider):
self.cusor = self.conn.cursor()
try:
self.cusor.execute('insert into qiubai values("%s","%s")'%(item['author'],item['content']))
self.conn.commit()
except Exception as e:
print(e)
self.conn.rollback()
return item # 返回给下一个即将被执行的管道类 def close_spider(self,spider):
self.cusor.close()
self.conn.close() class RedisPipeLine(object):
conn = None
def open_spider(self,spider):
self.conn = Redis(host='127.0.0.1',port=6379)
def process_item(self,item,spider):
dic = {
'author':item['author'],
'content':item['content']
}
self.conn.lpush('qiubaiData',dic)

在settings配置文件中开启管道

# 开启管道,让管道类生效
ITEM_PIPELINES = {
'firstBlood.pipelines.FirstbloodPipeline': 300, # 数值表示优先级
'firstBlood.pipelines.MySqlPipeline': 301,
'firstBlood.pipelines.RedisbloodPipeline': 303,
}

使用scrapy爬取chouti的热点内容与发布者

(递归爬取解析多页页面数据)

import scrapy

from choutiPro.items import ChoutiproItem
class ChoutiSpider(scrapy.Spider):
name = 'chouti'
# allowed_domains = ['www.xxx.com']
start_urls = ['https://dig.chouti.com/r/scoff/hot/1'] #定义一个通用的url模板
url = 'https://dig.chouti.com/r/scoff/hot/%d'
pageNum = 1 def parse(self, response):
div_list = response.xpath('//div[@id="content-list"]/div')
for div in div_list:
content = div.xpath('./div[2]/div[1]/a/text()').extract_first()
author = div.xpath('./div[2]/div[2]/a[4]/b/text()').extract_first() item = ChoutiproItem()
item['author'] = author
item['content'] = content yield item #进行手动请求的发送
if self.pageNum <= 120:
self.pageNum += 1
new_url = format(self.url%self.pageNum)
yield scrapy.Request(url=new_url,callback=self.parse)

scrapy框架基础的更多相关文章

  1. 爬虫入门之Scrapy 框架基础功能(九)

    Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛. 框架的力量,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网页内容以及各种图片,非 ...

  2. 网络爬虫值scrapy框架基础

    简介 Scrapy是一个高级的Python爬虫框架,它不仅包含了爬虫的特性,还可以方便的将爬虫数据保存到csv.json等文件中. 首先我们安装Scrapy. 其可以应用在数据挖掘,信息处理或存储历史 ...

  3. 爬虫入门之Scrapy框架基础rule与LinkExtractors(十一)

    1 parse()方法的工作机制: 1. 因为使用的yield,而不是return.parse函数将会被当做一个生成器使用.scrapy会逐一获取parse方法中生成的结果,并判断该结果是一个什么样的 ...

  4. 爬虫入门之Scrapy框架基础框架结构及腾讯爬取(十)

    Scrapy终端是一个交互终端,我们可以在未启动spider的情况下尝试及调试代码,也可以用来测试XPath或CSS表达式,查看他们的工作方式,方便我们爬取的网页中提取的数据. 如果安装了 IPyth ...

  5. python之scrapy框架基础搭建

    一.创建工程 #在命令行输入scrapy startproject xxx #创建项目 二.写item文件 #写需要爬取的字段名称 name = scrapy.Field() #例 三.进入spide ...

  6. 解读Scrapy框架

    Scrapy框架基础:Twsited Scrapy内部基于事件循环的机制实现爬虫的并发.原来: url_list = ['http://www.baidu.com','http://www.baidu ...

  7. 爬虫基础(五)-----scrapy框架简介

    ---------------------------------------------------摆脱穷人思维 <五> :拓展自己的视野,适当做一些眼前''无用''的事情,防止进入只关 ...

  8. 爬虫(九)scrapy框架简介和基础应用

    概要 scrapy框架介绍 环境安装 基础使用 一.什么是Scrapy? Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,非常出名,非常强悍.所谓的框架就是一个已经被集成了各种功能 ...

  9. 10.scrapy框架简介和基础应用

    今日概要 scrapy框架介绍 环境安装 基础使用 今日详情 一.什么是Scrapy? Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,非常出名,非常强悍.所谓的框架就是一个已经被 ...

随机推荐

  1. 145. Binary Tree Postorder Traversal (Stack, Tree)

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  2. Hibernate核心API

    ------------------------siwuxie095 (一)Configuration 1.一般情况 或: 加载核心配置文件:在 src 下找到名称为 Hibernate.cfg.xm ...

  3. RedHat 6 下配置网卡IP地址,Virtual Linux下配置网卡IP

    经常用到,自己Mark一下,顺带给需要的人参考. 1.配置文件修改 $ vi /etc/sysconfig/network-scripts/ifcfg-eth0 内容: DEVICE="et ...

  4. transform详解

    1.简介 该算法用于实行容器元素的变换操作.有如下两个使用原型,一个将迭代器区间[first,last)中元素,执行一元函数对象op操作,交换后的结果放在[result,result+(last-fi ...

  5. python实现高效率的排列组合算法-乾颐堂

    组合算法 本程序的思路是开一个数组,其下标表示1到m个数,数组元素的值为1表示其下标 代表的数被选中,为0则没选中. 首先初始化,将数组前n个元素置1,表示第一个组合为前n个数. 然后从左到右扫描数组 ...

  6. python中执行命令的3种方法小结-乾颐堂

    目前我使用到的python中执行cmd的方式有三种: 1. 使用os.system("cmd") 特点是执行的时候程序会打出cmd在linux上执行的信息. import os o ...

  7. 【转】The most comprehensive Data Science learning plan for 2017

    I joined Analytics Vidhya as an intern last summer. I had no clue what was in store for me. I had be ...

  8. Mybatis:传入参数方式以及#{}与${}的区别

    一.在MyBatis的select.insert.update.delete这些元素中都提到了parameterType这个属性.MyBatis现在可以使用的parameterType有基本数据类型和 ...

  9. es学习-索引配置

    1.创建一个新的索引并且添加一个配置 2.更新索引配置:(更新分词器为例子) 更新分词器前,一定要关闭索引,然后更新,最后再次开启索引 url:PUT http://127.0.0.1:9200/su ...

  10. 从零开始学习前端JAVASCRIPT — 11、JavaScript运动模型及轮播图效果、放大镜效果、自适应瀑布流

    未完待续...... 一.运动原理 通过连续不断的改变物体的位置,而发生移动变化. 使用setInterval实现. 匀速运动:速度值一直保持不变. 多物体同时运动:将定时器绑设置为对象的一个属性. ...