Splash的安装以及简单实用示例

📂 365bet体育在线游戏 ⏳ 2026-06-18 23:44:10 👽 admin 👁️ 9764 💾 620
Splash的安装以及简单实用示例

Splash 是一个用于执行带有 JavaScript 的网页渲染的工具,常用于网页抓取,特别是在页面包含动态内容的情况下。它可以通过 Docker 安装,下面是安装和一些常见示例的操作步骤。

1. 安装 Splash

使用 Docker 安装

最简单的方法是使用 Docker 安装 Splash,可以通过以下命令来安装:

sudo docker pull scrapinghub/splash

会拉取 Splash 的最新版本。

启动 Splash 容器

可以使用以下命令启动 Splash 容器,开放端口 8050:

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

会在后台运行 Splash,并将 8050 端口映射到宿主机。

2. 访问 Splash 服务

Splash 启动后,可以通过浏览器访问 http://localhost:8050,它提供了一个简单的 Web UI 和 API 文档。

也可以通过 curl 或代码调用 API,例如:

curl http://localhost:8050/render.html?url=http://example.com

3. 常用 API 示例

Splash 提供了多种 API 用于不同的渲染需求。以下是一些常用的示例:

3.1 渲染 HTML

渲染网页并返回网页的 HTML 内容:

curl http://localhost:8050/render.html?url=https://example.com

示例结果会返回 https://example.com 的完整 HTML 内容。

3.2 渲染 PNG

将网页渲染为 PNG 图片并保存:

curl http://localhost:8050/render.png?url=https://example.com > result.png

会将 example.com 的网页渲染为 PNG 图片并保存为 result.png。

3.3 渲染 JPEG

将网页渲染为 JPEG 图片:

curl http://localhost:8050/render.jpeg?url=https://example.com > result.jpg

这会将网页渲染为 JPEG 格式的图片并保存。

3.4 渲染 JSON

如果希望渲染网页并提取一些信息(如标题、HTML 片段等),可以使用 JSON 格式返回渲染结果:

curl http://localhost:8050/render.json?url=https://example.com

它会返回网页的元数据和截图等信息:

{

"html": "...",

"png": "data:image/png;base64,...",

"url": "https://example.com",

"requestedUrl": "https://example.com"

}

3.5 设置渲染等待时间

有些页面需要等待一段时间才能完全加载,可以使用 wait 参数来设置等待时间(单位:秒):

curl 'http://localhost:8050/render.html?url=https://example.com&wait=2'

这个请求会等待 2 秒后再返回渲染结果,适用于处理需要时间加载的动态页面。

3.6 执行自定义 JavaScript

可以在渲染过程中执行自定义 JavaScript。例如,点击页面中的按钮或滚动到页面底部:

curl 'http://localhost:8050/execute?url=https://example.com&lua_source=function main(splash) splash:go(splash.args.url) splash:wait(2) return {html = splash:html()} end'

这个示例会等待 2 秒,然后返回页面的 HTML 内容。

4. Lua 脚本支持

Splash 还支持使用 Lua 脚本编写复杂的渲染逻辑,下面是一个简单的 Lua 脚本示例,它会打开一个网页、等待一段时间,并返回页面 HTML 和截图:

function main(splash)

splash:go("https://example.com")

splash:wait(3)

return {

html = splash:html(),

png = splash:png()

}

end

可以将这个脚本发送到 Splash 来执行:

curl -X POST -F 'lua_source=function main(splash) splash:go("https://example.com") splash:wait(3) return {html=splash:html(), png=splash:png()} end' http://localhost:8050/execute

这个请求会返回 HTML 和 PNG 格式的截图。

5. 调整视图大小

你可以调整浏览器窗口的大小(宽度和高度):

curl 'http://localhost:8050/render.png?url=https://example.com&width=800&height=600' > result.png

这会将浏览器的宽度设置为 800 像素,高度设置为 600 像素,并生成截图。

6. 禁用图片加载

如果不需要加载网页中的图片,可以通过禁用图片加速渲染:

curl 'http://localhost:8050/render.html?url=https://example.com&images=0'

7. 设置用户代理

可以通过 user_agent 参数自定义请求的用户代理(User-Agent):

curl 'http://localhost:8050/render.html?url=https://example.com&user_agent=Mozilla/5.0'

8. 设置 HTTP Headers

有时可能需要自定义 HTTP 请求头,比如添加自定义 Cookie:

curl 'http://localhost:8050/render.html?url=https://example.com&headers={"Cookie": "name=value"}'

总结

Splash 是一个强大的工具,适用于需要执行 JavaScript 渲染的动态页面抓取。可以使用它的各种 API 来渲染 HTML、图片、JSON 等内容,并可以通过 Lua 脚本实现复杂的交互和自动化操作。

相关数据包

​结婚五周年为什么叫木婚

​结婚五周年为什么叫木婚

📅 09-24 🔗 365bet体育在线游戏
如何用画图工具截图

如何用画图工具截图

📅 10-29 🔗 足球365是什么意思
京东闪付怎么样?跟白条闪付有什么区别

京东闪付怎么样?跟白条闪付有什么区别

📅 09-03 🔗 best365官网登录
铁拳6怎么双人对打(铁拳6街机怎么双人)

铁拳6怎么双人对打(铁拳6街机怎么双人)

📅 07-07 🔗 best365官网登录
← 文字转图片 淘宝同样的东西价格差距怎么那么大?有什么原因? →