Scrapy-Splash的介紹、安裝以及實例

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的特色以下:css

  • 並行處理多個網頁
  • 獲得HTML結果以及(或者)渲染成圖片
  • 關掉加載圖片或使用 Adblock Plus規則使得渲染速度更快
  • 使用JavaScript處理網頁內容
  • 使用Lua腳本
  • 能在Splash-Jupyter Notebooks中開發Splash Lua scripts
  • 可以得到具體的HAR格式的渲染信息

scrapy-splash的安裝

  因爲Splash的上述特色,使得Splash和Scrapy二者的兼容性較好,抓取效率較高。
  聽了上面的介紹,有沒有對scrapy-splash很心動呢?下面就介紹如何安裝scrapy-splash,步驟以下:
  1. 安裝scrapy-splash模塊html

pip3 install scrapy-splash

  2. scrapy-splash使用的是Splash HTTP API, 因此須要一個splash instance,通常採用docker運行splash,因此須要安裝docker。不一樣系統的安裝命令會不一樣,如筆者的CentOS7系統的安裝方式爲:python

sudo yum install docker

安裝完docker後,能夠輸入命令‘docker -v’來驗證docker是否安裝成功。git

  3. 開啓docker服務,拉取splash鏡像(pull the image):github

sudo service docker start
sudo dock pull scrapinghub/splash

運行結果以下:web

pull the image

  4. 開啓容器(start the container):docker

sudo docker run -p 8050:8050 scrapinghub/splash

此時Splash以運行在本地服務器的端口8050(http).在瀏覽器中輸入'localhost:8050', 頁面以下:api

這裏寫圖片描述

在這個網頁中咱們可以運行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'

具體的配置說明能夠參考:  https://pypi.python.org/pypi/... .

  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, 結果以下:

這裏寫圖片描述

  實例展現到此結束,歡迎你們訪問這個項目的Github地址:  https://github.com/percent4/p... .固然,有什麼問題,也能夠載下面留言評論哦~~

相關文章
相關標籤/搜索