1. 程式人生 > >[Python3網絡爬蟲開發實戰] 1.8.3-Scrapy-Splash的安裝

[Python3網絡爬蟲開發實戰] 1.8.3-Scrapy-Splash的安裝

original plugin 5.4 ima asc spl python min 8.4

Scrapy-Splash是一個Scrapy中支持JavaScript渲染的工具,本節來介紹它的安裝方式。

Scrapy-Splash的安裝分為兩部分。一個是Splash服務的安裝,具體是通過Docker,安裝之後,會啟動一個Splash服務,我們可以通過它的接口來實現JavaScript頁面的加載。另外一個是Scrapy-Splash的Python庫的安裝,安裝之後即可在Scrapy中使用Splash服務。

1. 相關鏈接

  • GitHub:https://github.com/scrapy-plugins/scrapy-splash
  • PyPI:https://pypi.python.org/pypi/scrapy-splash
  • 使用說明:https://github.com/scrapy-plugins/scrapy-splash#configuration
  • Splash官方文檔:http://splash.readthedocs.io

2. 安裝Splash

Scrapy-Splash會使用Splash的HTTP API進行頁面渲染,所以我們需要安裝Splash來提供渲染服務。這裏通過Docker安裝,在這之前請確保已經正確安裝好了Docker。

安裝命令如下:

1 docker run -p 8050:8050 scrapinghub/splash

安裝完成之後,會有類似的輸出結果:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 2017-07-03 08:53:28+0000 [-] Log opened. 2017-07-03 08:53:28.447291 [-] Splash version: 3.0 2017-07-03 08:53:28.452698 [-] Qt 5.9.1, PyQt 5.9, WebKit 602.1, sip 4.19.3, Twisted 16.1.1, Lua 5.2 2017-07-03 08:53:28.453120 [-] Python 3.5.2 (default, Nov 17 2016, 17:05:23) [GCC 5.4.0 20160609]
2017-07-03 08:53:28.453676 [-] Open files limit: 1048576 2017-07-03 08:53:28.454258 [-] Can‘t bump open files limit 2017-07-03 08:53:28.571306 [-] Xvfb is started: [‘Xvfb‘, ‘:1599197258‘, ‘-screen‘, ‘0‘, ‘1024x768x24‘, ‘-nolisten‘, ‘tcp‘] QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to ‘/tmp/runtime-root‘ 2017-07-03 08:53:29.041973 [-] proxy profiles support is enabled, proxy profiles path: /etc/splash/proxy-profiles 2017-07-03 08:53:29.315445 [-] verbosity=1 2017-07-03 08:53:29.315629 [-] slots=50 2017-07-03 08:53:29.315712 [-] argument_cache_max_entries=500 2017-07-03 08:53:29.316564 [-] Web UI: enabled, Lua: enabled (sandbox: enabled) 2017-07-03 08:53:29.317614 [-] Site starting on 8050 2017-07-03 08:53:29.317801 [-] Starting factory <twisted.web.server.Site object at 0x7ffaa4a98cf8>

這樣就證明Splash已經在8050端口上運行了。這時我們打開http://localhost:8050,即可看到Splash的主頁,如圖1-80所示。

技術分享圖片圖1-80 運行頁面

當然,Splash也可以直接安裝在遠程服務器上。我們在服務器上以守護態運行Splash即可,命令如下:

1 docker run -d -p 8050:8050 scrapinghub/splash

這裏多了-d參數,它代表將Docker容器以守護態運行,這樣在中斷遠程服務器連接後,不會終止Splash服務的運行。

3. Scrapy-Splash的安裝

成功安裝Splash之後,接下來再來安裝其Python庫,命令如下:

1 pip3 install scrapy-splash

命令運行完畢後,就會成功安裝好此庫,後面會詳細介紹它的用法。

[Python3網絡爬蟲開發實戰] 1.8.3-Scrapy-Splash的安裝