爬虫scrapy框架 2018-10-31

2018-10-31  本文已影响0人  Mr_Du_Biao

一、scrapy框架介绍

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

二、安装

Linux安装:

Windows安装:

三、基础使用

项目结构:

project_name/
   scrapy.cfg:
   project_name/
       __init__.py
       items.py
       pipelines.py
       settings.py
       spiders/
           __init__.py

scrapy.cfg   项目的主配置信息。(真正爬虫相关的配置信息在settings.py文件中)
items.py     设置数据存储模板,用于结构化数据,如:Django的Model
pipelines    数据持久化处理
settings.py  配置文件,如:递归的层数、并发数,延迟下载等
spiders      爬虫目录,如:创建文件,编写爬虫解析规则
# -*- coding: utf-8 -*-
import scrapy

class QiubaiSpider(scrapy.Spider):
    name = 'qiubai' #应用名称
    #允许爬取的域名(如果遇到非该域名的url则爬取不到数据)
    allowed_domains = ['https://www.qiushibaike.com/']
    #起始爬取的url
    start_urls = ['https://www.qiushibaike.com/']

     #访问起始URL并获取结果后的回调函数,该函数的response参数就是向起始的url发送请求后,获取的响应对象.该函数返回值必须为可迭代对象或者NUll 
     def parse(self, response):
        print(response.text) #获取字符串类型的响应内容
        print(response.body)#获取字节类型的相应内容
修改内容及其结果如下:
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协议

四.小试牛刀:将糗百首页中段子的内容和标题进行爬取

# -*- coding: utf-8 -*-
import scrapy

class QiubaiSpider(scrapy.Spider):
    name = 'qiubai'
    allowed_domains = ['https://www.qiushibaike.com/']
    start_urls = ['https://www.qiushibaike.com/']

    def parse(self, response):
        #xpath为response中的方法,可以将xpath表达式直接作用于该函数中
        odiv = response.xpath('//div[@id="content-left"]/div')
        content_list = [] #用于存储解析到的数据
        for div in odiv:
            #xpath函数返回的为列表,列表中存放的数据为Selector类型的数据。我们解析到的内容被封装在了Selector对象中,需要调用extract()函数将解析的内容从Selecor中取出。
            author = div.xpath('.//div[@class="author clearfix"]/a/h2/text()')[0].extract()
            content=div.xpath('.//div[@class="content"]/span/text()')[0].extract()

            #将解析到的内容封装到字典中
            dic={
                '作者':author,
                '内容':content
            }
            #将数据存储到content_list这个列表中
            content_list.append(dic)

        return content_list

执行爬虫程序:

执行输出指定格式进行存储:将爬取到的数据写入不同格式的文件中进行存储
    scrapy crawl qiubai -o qiubai.json
    scrapy crawl qiubai -o qiubai.xml
    scrapy crawl qiubai -o qiubai.csv

五.scrapy持久化操作:将爬取到糗百数据存储写入到文本文件中进行存储

1、自己手动存储

# -*- coding: utf-8 -*-
import scrapy


class QiubaiSpider(scrapy.Spider):
    name = 'qiubai'
    allowed_domains = ['https://www.qiushibaike.com/']
    start_urls = ['https://www.qiushibaike.com/']

    def parse(self, response):
        #xpath为response中的方法,可以将xpath表达式直接作用于该函数中
        odiv = response.xpath('//div[@id="content-left"]/div')
        with open('./data.txt', 'w') as fp:
            for div in odiv:
                 #xpath函数返回的为列表,列表中存放的数据为Selector类型的数据。我们解析到的内容被封装在了Selector对象中,需要调用extract()函数将解析的内容从Selecor中取出。
                 author = div.xpath('.//div[@class="author clearfix"]/a/h2/text()')[0].extract()
                 content=div.xpath('.//div[@class="content"]/span/text()')[0].extract()
                 #持久化存储爬取到的内容
                 fp.write(author + ':' + content + '\n')

注意:上述代码表示的持久化操作是我们自己通过IO操作将数据进行的文件存储。在scrapy框架中已经为我们专门集成好了高效、便捷的持久化操作功能,我们直接使用即可。要想使用scrapy的持久化操作功能,我们首先来认识如下两个文件:

items.py:数据结构模板文件。定义数据属性。
    pipelines.py:管道文件。接收数据(items),进行持久化操作。

持久化流程:
    1.爬虫文件爬取到数据后,需要将数据封装到items对象中。
    2.使用yield关键字将items对象提交给pipelines管道进行持久化操作。
    3.settings.py配置文件中开启管道

2、用items和管道进行持久化

# -*- coding: utf-8 -*-
import scrapy
from secondblood.items import SecondbloodItem

class QiubaidemoSpider(scrapy.Spider):
    name = 'qiubaiDemo'
    allowed_domains = ['www.qiushibaike.com']
    start_urls = ['http://www.qiushibaike.com/']

    def parse(self, response):
        odiv = response.xpath('//div[@id="content-left"]/div')
        for div in odiv:
            # xpath函数返回的为列表,列表中存放的数据为Selector类型的数据。我们解析到的内容被封装在了Selector对象中,需要调用extract()函数将解析的内容从Selecor中取出。
            author = div.xpath('.//div[@class="author clearfix"]//h2/text()').extract_first()
            author = author.strip('\n')#过滤空行
            content = div.xpath('.//div[@class="content"]/span/text()').extract_first()
            content = content.strip('\n')#过滤空行

            #将解析到的数据封装至items对象中
            item = SecondbloodItem()
            item['author'] = author
            item['content'] = content

            yield item#提交item到管道文件(pipelines.py)
import scrapy

class SecondbloodItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    author = scrapy.Field() #存储作者
    content = scrapy.Field() #存储段子内容
# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html


class SecondbloodPipeline(object):
    #构造方法
    def __init__(self):
        self.fp = None  #定义一个文件描述符属性
  #下列都是在重写父类的方法:
    #开始爬虫时,执行一次
    def open_spider(self,spider):
        print('爬虫开始')
        self.fp = open('./data.txt', 'w')

   #因为该方法会被执行调用多次,所以文件的开启和关闭操作写在了另外两个只会各自执行一次的方法中。
    def process_item(self, item, spider):
        #将爬虫程序提交的item进行持久化存储
        self.fp.write(item['author'] + ':' + item['content'] + '\n')
        return item

    #结束爬虫时,执行一次
    def close_spider(self,spider):
        self.fp.close()
        print('爬虫结束')
class MysqlPipeLine(object):
    conn = None
    cursor = None
    def open_spider(self, spider):
        print('mysql 开始爬虫')
        self.conn = pymysql.connect(host="127.0.0.1",port=3306,db='qiubai',user="root",charset='utf8')
    def process_item(self, item, spider):
        sql = "insert into t_qiubai values('%s','%s')"%(item['author'],item['content'])
        self.cursor = self.conn.cursor()
        try:
            self.cursor.execute(sql)
            self.conn.commit()
        except Exception as e:
            print(e)
            self.conn.rollback()

    def close_spider(self, spider):
        print("mysql 结束爬虫")
        self.cursor.close()
        self.conn.close()
class redisPipeLine(object):
    r = None
    def open_spider(self, spider):
        print('redis 开始爬虫')
        self.r = redis.Redis(host='127.0.0.1',port=6379)

    def process_item(self, item, spider):
        dict = {
            'author':item['author'],
            'content':item['content']
        }
        print(self.r)
        self.r.lpush('data',dict)

    def close_spider(self, spider):
        print("redis 结束爬虫")
import pymongo
class MongodbPipeLine(object):
    mongo_db = None
    def open_spider(self, spider):
        print("爬虫开始")
        mongo_client = pymongo.MongoClient(host="127.0.0.1", port=27017)
        self.mongo_db = mongo_client["qiushi"]

    def process_item(self, item, spider):
        self.mongo_db.qiushi.insert_one({'author':item['author'], "content": item['content']})
        return item

    def close_spider(self, spider):

        print("爬虫结束")


#开启管道
ITEM_PIPELINES = {
    'secondblood.pipelines.SecondbloodPipeline': 300, #300表示为优先级,值越小优先级越高
}

六、Scrapy递归爬取多页数据:

1、循环获取网页上的图片

# -*- coding: utf-8 -*-
import scrapy
from qiutuPro.items import QiutuproItem

class QiutuSpider(scrapy.Spider):
    name = 'qiutu'
    #allowed_domains = ['www.qiushibaike.com/pic']
    start_urls = ['https://www.qiushibaike.com/pic/']

    #获取图片二进制的数据值
    def parse_img(self,response):

        img_data = response.body
        item = QiutuproItem()
        item['data'] = img_data
        item['url'] = response.url
        yield item

    def parse(self, response):
        div_list = response.xpath('//*[@id="content-left"]/div')
        for div in div_list:
            img_url = "https:"+div.xpath('./div[2]/a/img/@src').extract_first()

            #进行手动请求的发送
            yield scrapy.Request(url=img_url,callback=self.parse_img)
import scrapy

class QiutuproItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    data = scrapy.Field()
    url = scrapy.Field()
class QiutuproPipeline(object):
    def process_item(self, item, spider):
        img_name = item['url'].split('/')[-1]
        with open(img_name,'wb') as fp:
            fp.write(item['data'])
            print(img_name+"下载成功")
        return item

2、 循环获取分页数据

# -*- coding: utf-8 -*-
import scrapy
from qiushiPro.items import QiushiproItem

class QiushiSpider(scrapy.Spider):
    name = 'qiushi'
    #allowed_domains = ['www.qiushibaike.com/text']
    start_urls = ['http://www.qiushibaike.com/text/']
    #url和pageNum形成一个通用的页面url
    url = 'https://www.qiushibaike.com/text/page/%s/'
    pageNum = 1

    def parse(self, response):
        div_list = response.xpath('//div[@id="content-left"]/div')

        for div in div_list:
            author = div.xpath('./div/a[2]/h2/text()').extract_first()
            content = div.xpath('./a/div/span/text()').extract_first()

            # 实例化item对象
            item = QiushiproItem()
            item['author'] = author or ""
            item['content'] = content

            #print(item['author']+":"+item['content'])
            # 将item对象提交给管道
            yield item

        if self.pageNum <=13:
            self.pageNum+=1
            url = format(self.url%self.pageNum)
            print(url+"+++++++++++++++++++++++++++++++++++++++")
            yield scrapy.Request(url=url,callback=self.parse)

七、Scrapy 发送post请求

父类中默认实现的形式(发起的起始url列表中列表元素对应url的get请求)

  def start_requests(self):
        for url in self.start_urls:
            yield scrapy.Request(url=url,callback=self.parse)

方法一、Scrapy 发送post请求就是对父类的start_requests方法进行重写,完成post请求的发送

 def start_requests(self):
        url = 'https://fanyi.baidu.com/sug'
        data = {
            'kw':'dog'
        }
        yield scrapy.FormRequest(url=url,formdata=data,callback=self.parse)
    def parse(self, response):
        print(response.text)

方法二:使用Request方法中的method和body参数

from scrapy.http.cookies import CookieJar
 # 1. 去第一次访问页面中获取cookie
        # print(response.headers['Set-Cookie'],type(response.headers['Set-Cookie']))
        cookie_jar = CookieJar() # 空
        cookie_jar.extract_cookies(response, response.request) # cookie_jar中包含了cookie


        for k, v in cookie_jar._cookies.items():
            for i, j in v.items():
                for m, n in j.items():
                    self.cookie_dict[m] = n.value
        # 2. 向https://dig.chouti.com/login发送POST请求
        yield Request(
            url='https://dig.chouti.com/login',
            method='POST',
            body="phone=8615901492719&password=qwer1234&oneMonth=1",
            cookies=self.cookie_dict,
            headers={
                'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
                'content-type':'application/x-www-form-urlencoded; charset=UTF-8',
            },
            callback=self.check_login
        )
上一篇下一篇

猜你喜欢

热点阅读