芝麻HTTP:Scrapy-Splash的安装

时间:2023-03-09 07:19:34
芝麻HTTP:Scrapy-Splash的安装

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

Scrapy-Splash的安装分为两部分。一个是Splash服务的安装,具体是通过Docker,安装之后,会启动一个Splash服务,我们可以通过它的接口来实现JavaScript页面的加载。另外一个是Scrapy-Splash的Python库的安装,安装之后即可在Scrapy中使用Splash服务。

1. 相关链接

2. 安装Splash

Scrapy-Splash会使用Splash的HTTP API进行页面渲染,所以我们需要安装Splash来提供渲染服务。这里通过Docker安装,在这之前请确保已经正确安装好了Docker。

安装命令如下:

docker run -p : scrapinghub/splash

安装完成之后,会有类似的输出结果:

-- ::+ [-] Log opened.
-- ::28.447291 [-] Splash version: 3.0
-- ::, PyQt , Twisted , Lua 5.2
-- :: ( , ::) [GCC  ]
-- ::
-- ::28.454258 [-] Can't bump open files limit
-- ::', '1024x768x24', '-nolisten', 'tcp']
QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root'
-- ::29.041973 [-] proxy profiles support is enabled, proxy profiles path: /etc/splash/proxy-profiles
-- ::
-- ::
-- ::
-- ::29.316564 [-] Web UI: enabled, Lua: enabled (sandbox: enabled)
-- ::
-- ::29.317801 [-] Starting factory <twisted.web.server.Site object at 0x7ffaa4a98cf8>

这样就证明Splash已经在8050端口上运行了。这时我们打开http://localhost:8050,即可看到Splash的主页,如图1-80所示。

芝麻HTTP:Scrapy-Splash的安装

图1 运行页面

当然,Splash也可以直接安装在远程服务器上。我们在服务器上以守护态运行Splash即可,命令如下:

docker run -d -p : scrapinghub/splash

这里多了-d参数,它代表将Docker容器以守护态运行,这样在中断远程服务器连接后,不会终止Splash服务的运行。

3. Scrapy-Splash的安装

成功安装Splash之后,接下来再来安装其Python库,命令如下:

pip3 install scrapy-splash

命令运行完毕后,就会成功安装好此库。