Scrapy爬虫(8)scrapy-splash的入门

Scrapy爬虫(8)scrapy-splash的入门

2018年03月17日 16:16:36 剑与星辰 阅读数:885 标签: scrapysplash爬虫 更多css

我的分类: scrapyhtml

版权声明:本文为博主原创文章,未经博主容许不得转载。 https://blog.csdn.net/jclian91/article/details/79592754python

scrapy-splash的介绍

  在前面的博客中,咱们已经见识到了Scrapy的强大之处。可是,Scrapy也有其不足之处,即Scrapy没有JS engine, 所以它没法爬取JavaScript生成的动态网页,只能爬取静态网页,而在现代的网络世界中,大部分网页都会采用JavaScript来丰富网页的功能。因此,这无疑Scrapy的遗憾之处。 
  那么,咱们还能愉快地使用Scrapy来爬取动态网页吗?有没有什么补充的办法呢?答案依然是yes!答案就是,使用scrapy-splash模块! 
  scrapy-splash模块主要使用了Splash. 所谓的Splash, 就是一个Javascript渲染服务。它是一个实现了HTTP API的轻量级浏览器,Splash是用Python实现的,同时使用Twisted和QT。Twisted(QT)用来让服务具备异步处理能力,以发挥webkit的并发能力。Splash的特色以下:git

  • 并行处理多个网页
  • 获得HTML结果以及(或者)渲染成图片
  • 关掉加载图片或使用 Adblock Plus规则使得渲染速度更快
  • 使用JavaScript处理网页内容
  • 使用Lua脚本
  • 能在Splash-Jupyter Notebooks中开发Splash Lua scripts
  • 可以得到具体的HAR格式的渲染信息

scrapy-splash的安装

  因为Splash的上述特色,使得Splash和Scrapy二者的兼容性较好,抓取效率较高。 
  听了上面的介绍,有没有对scrapy-splash很心动呢?下面就介绍如何安装scrapy-splash,步骤以下: 
  1. 安装scrapy-splash模块github

pip3 install scrapy-splash
  • 1

  2. scrapy-splash使用的是Splash HTTP API, 因此须要一个splash instance,通常采用docker运行splash,因此须要安装docker。不一样系统的安装命令会不一样,如笔者的CentOS7系统的安装方式为:web

sudo yum install docker
  • 1

安装完docker后,能够输入命令‘docker -v’来验证docker是否安装成功。docker

  3. 开启docker服务,拉取splash镜像(pull the image):api

sudo service docker start
sudo dock pull scrapinghub/splash
  • 1
  • 2

运行结果以下:浏览器

 


pull the image

 

  4. 开启容器(start the container):服务器

sudo docker run -p 8050:8050 scrapinghub/splash
  • 1

此时Splash以运行在本地服务器的端口8050(http).在浏览器中输入’localhost:8050’, 页面以下:

 


这里写图片描述

 

在这个网页中咱们可以运行Lua scripts,这对咱们在scrapy-splash中使用Lua scripts是很是有帮助的。以上就是咱们安装scrapy-splash的所有。

scrapy-splash的实例

  在安装完scrapy-splash以后,不趁机介绍一个实例,实在是说不过去的,咱们将在此介绍一个简单的实例,那就是利用百度查询手机号码信息。好比,咱们在百度输入框中输入手机号码‘159********’,而后查询,获得以下信息:

 


这里写图片描述 

 

咱们将利用scrapy-splash模拟以上操做并获取手机号码信息。

  1. 建立scrapy项目phone 
  2. 配置settings.py文件,配置的内容以下:

ROBOTSTXT_OBEY = False

SPIDER_MIDDLEWARES = {
    'scrapy_splash.SplashDeduplicateArgsMiddleware': 100,
}

DOWNLOADER_MIDDLEWARES = {
    'scrapy_splash.SplashCookiesMiddleware': 723,
    'scrapy_splash.SplashMiddleware': 725,
    'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware': 810
}

SPLASH_URL = 'http://localhost:8050'

DUPEFILTER_CLASS = 'scrapy_splash.SplashAwareDupeFilter'
HTTPCACHE_STORAGE = 'scrapy_splash.SplashAwareFSCacheStorage'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

具体的配置说明能够参考:  https://pypi.python.org/pypi/scrapy-splash .

  3. 建立爬虫文件phoneSpider.py, 代码以下:

# -*- coding: utf-8 -*-
from scrapy import Spider, Request
from scrapy_splash import SplashRequest

# splash lua script
script = """
         function main(splash, args)
             assert(splash:go(args.url))
             assert(splash:wait(args.wait))
             js = string.format("document.querySelector('#kw').value=%s;document.querySelector('#su').click()", args.phone)
             splash:evaljs(js)
             assert(splash:wait(args.wait))
             return splash:html()
         end
         """

class phoneSpider(Spider):
    name = 'phone'
    allowed_domains = ['www.baidu.com']
    url = 'https://www.baidu.com'

    # start request
    def start_requests(self):
        yield SplashRequest(self.url, callback=self.parse, endpoint='execute', args={'lua_source': script, 'phone':'159*******', 'wait': 5})

    # parse the html content 
    def parse(self, response):
        info = response.css('div.op_mobilephone_r.c-gap-bottom-small').xpath('span/text()').extract()
        print('='*40)
        print(''.join(info))
        print('='*40)
  • 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

  4. 运行爬虫,scrapy crawl phone, 结果以下:

 


这里写图片描述 

 

  实例展现到此结束,欢迎你们访问这个项目的Github地址:  https://github.com/percent4/phoneSpider .固然,有什么问题,也能够载下面留言评论哦~~

相关文章
相关标签/搜索