ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

splash-简介及入门

2019-08-23 15:06:06  阅读:408  来源: 互联网

标签:http 入门 render 渲染 简介 args html splash


 splash

 

1.      splash简介

Splash是一个JavaScript渲染服务,是一个带有HTTP API的轻量级浏览器,同时它对接了Python中的Twisted和QT库。利用它,我们同样可以实现动态渲染页面的抓取。

 

 

利用Splash,我们可以实现如下功能:

异步方式处理多个网页渲染过程;

获取渲染后的页面的源代码或截图;

通过关闭图片渲染或者使用Adblock规则来加快页面渲染速度;

可执行特定的JavaScript脚本;

可通过Lua脚本来控制页面渲染过程;

获取渲染的详细过程并通过HAR(HTTP Archive)格式呈现。

 

2.      运行及使用

2.1.    运行

启动docker,实验位于windows平台,点击Docker Quickstart Terminal即可。

 

拉取镜像splash

$ docker pull scrapinghub/splash

启动容器:

$ docker run -p 8050:8050 scrapinghub/splash

表示:Splash现在在端口8050(http)可用。

 

2.2.    使用

浏览器访问http://192.168.99.100:8050

结果是splash界面。

 

释义:

左侧为简介,略;

右侧上层为目标网页地址,默认为baidu.com;

右侧下层为当前解析脚本,它使用lua语言。

 

输入目标网页地址:天眼查

https://www.tianyancha.com/search?key=%E9%98%BF%E9%87%8C%E5%B7%B4%E5%B7%B4

然后render me,结果如下:

 

输出内容由lua脚本指定。

 

默认输出三种信息,分别是html,png,har。

实际上是Splash执行了整个网页的渲染过程,包括CSS、JavaScript的加载等过程,呈现的页面和浏览器中得到的结果完全一致,然后返回渲染截图、HAR加载统计数据、网页的源代码。

当然可以定制脚本,但一般不需要写lua脚本,有相关的python包。

 

3.      lua脚本

 

默认脚本如下:

function main(splash, args)

  assert(splash:go(args.url))

  assert(splash:wait(0.5))

  return {

    html = splash:html(),

    png = splash:png(),

    har = splash:har(),

  }

end

 

4.      总结

总而言之,splash是一个渲染引擎。

输入url,根据lua脚本返回相应对象。

它能在js执行前执行本地js(用于环境模拟)。

 

5.      接口

通常使用的是http api,所以,这里提及的都是http接口。

参考文档:https://splash.readthedocs.io/en/stable/api.html#render-html

http接口使用get方法访问即可。

 

概念:

endpoint:执行主体,决定返回的数据类型。常用的有execute,run,render.html,render.png。

 

5.1.    返回的数据类型

render.html

Return the HTML of the javascript-rendered page.

示例:curl 'http://localhost:8050/render.html?url=http://domain.com/page-with-javascript.html&timeout=10&wait=0.5'

 

render.png

Return an image (in PNG format) of the javascript-rendered page.

render.jpeg

Return an image (in JPEG format) of the javascript-rendered page.

示例:

# render with default quality

curl 'http://localhost:8050/render.jpeg?url=http://domain.com/'

# render with low quality

curl 'http://localhost:8050/render.jpeg?url=http://domain.com/&quality=30'

 

render.har

Return information about Splash interaction with a website in HAR format. It includes information about requests made, responses received, timings, headers, etc.

返回HAR格式数据,包括请示生成,响应,时间线,头部等。

 

render.json

Return a json-encoded dictionary with information about javascript-rendered webpage. It can include HTML, PNG and other information, based on arguments passed.

Return a json-encoded dictionary with information about javascript-rendered webpage. It can include HTML, PNG and other information, based on arguments passed.

 

重点:

execute

Execute a custom rendering script and return a result.

run

它与execute相同,但接口形式不同,简单来说,不需要包裹函数体。

This endpoint is the same as execute, but it wraps lua_source in function main(splash, args) ... endautomatically. For example, if you’re sending this script to execute:

在execute中应该这样写:

function main(splash, args)

    assert(splash:go(args.url))

    assert(splash:wait(1.0))

    return splash:html()

end

equivalent script for run endpoint would be

它等效于run

assert(splash:go(args.url))

assert(splash:wait(1.0))

return splash:html()

 

executer的参数

Arguments:

lua_source : string : required(重点)

Browser automation script. See Splash Scripts Tutorial for more info.

timeout : float : optional

Same as ‘timeout’ argument for render.html.

allowed_domains : string : optional

Same as ‘allowed_domains’ argument for render.html.

proxy : string : optional

Same as ‘proxy’ argument for render.html.

filters : string : optional

Same as ‘filters’ argument for render.html.

save_args : JSON array or a comma-separated string : optional

Same as ‘save_args’ argument for render.html. Note that you can save not only default Splash arguments, but any other parameters as well.

load_args : JSON object or a string : optional

Same as ‘load_args’ argument for render.html. Note that you can load not only default Splash arguments, but any other parameters as well.

 

标签:http,入门,render,渲染,简介,args,html,splash
来源: https://www.cnblogs.com/wodeboke-y/p/11400021.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有