美文网首页爬虫专题大数据 爬虫Python AI Sql
Python爬虫(18)Scrapy-Splash的介绍,安装以

Python爬虫(18)Scrapy-Splash的介绍,安装以

作者: 山阴少年 | 来源:发表于2018-03-17 16:19 被阅读216次

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的特点如下:

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

scrapy-splash的安装

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

pip3 install scrapy-splash

  2. scrapy-splash使用的是Splash HTTP API, 所以需要一个splash instance,一般采用docker运行splash,所以需要安装docker。不同系统的安装命令会不同,如笔者的CentOS7系统的安装方式为:

sudo yum install docker

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

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

sudo service docker start
sudo dock pull scrapinghub/splash

运行结果如下:

pull the image

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

sudo docker run -p 8050:8050 scrapinghub/splash

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

splash

在这个网页中我们能够运行Lua scripts,这对我们在scrapy-splash中使用Lua scripts是非常有帮助的。以上就是我们安装scrapy-splash的全部。

scrapy-splash的实例

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

Baidu phone information

我们将利用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'

具体的配置说明可以参考:  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)

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

result

  实例展示到此结束,欢迎大家访问这个项目的Github地址:  https://github.com/percent4/phoneSpider .当然,有什么问题,也可以载下面留言评论哦~~

相关文章

  • Python爬虫(18)Scrapy-Splash的介绍,安装以

    scrapy-splash的介绍   在前面的博客中,我们已经见识到了Scrapy的强大之处。但是,Scrapy也...

  • Scrapy框架之Scrapy-Splash的使用

    Scrapy-Splash插件的介绍与安装, 最后通过一个实际的例子介绍Scrapy-Splash的使用 前提 熟...

  • Python爬虫Scrapy-Splash安装及使用

    Scrapy-Splash是一个Scrapy中支持JavaScript渲染的工具,本节来介绍他的安装方式。Scra...

  • Python安装教程

    学习Python爬虫前,要先安装Python环境,下面就简要介绍两种方法,来获得Python安装程序包。 1、去P...

  • 关于scrapy-splash使用以及如何设置代理ip

    首先我们先介绍下如何使用scrapy-splash: 1、安装:$ pip install scrapy-spla...

  • 爬虫入门基础

    Day01 一、爬虫介绍 什么是爬虫 Python爬虫的优势 Python爬虫需要掌握什么 爬虫与反爬虫与反反爬虫...

  • python 爬虫框架Scrapy使用方法

    Python作为脚本语言,功能非常强大,这里介绍一下爬虫框架Scrapy有安装及使用方法介绍。 一、安装篇 pyt...

  • Python网络爬虫

    Python开发简单爬虫(Python2.X版本,Eclipse工具) 一、爬虫介绍 爬虫调度端:启动、停止爬虫,...

  • 01-认识爬虫

    一、爬虫介绍 什么是爬虫 Python爬虫的优势 Python爬虫需要掌握什么 爬虫与反爬虫与反反爬虫三角之争 网...

  • Pyspider-安装

    简介 pyspider是国人写的一个python的爬虫库,简单介绍下我的安装环境.win10系统,python的版...

网友评论

    本文标题:Python爬虫(18)Scrapy-Splash的介绍,安装以

    本文链接:https://www.haomeiwen.com/subject/coyjqftx.html