Splash基础认识

1
2
3
4
5
6
7
8
9
10
-- Splash可以采用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

API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# render.html获取js渲染页面的html代码
url = 'http://localhost:8050/render.html?url=https://www.baidu.com&wait=5'
# render.png/render.jpeg 返回页面截图
# render.har 返回页面加载过程信息
# render.json 可包含前面3种信息 需自行添加html=1,har=1,png=1等等
url = 'http://localhost:8050/render.json?url=https://www.baidu.com&html=1&har=1&png=1'
response = requests.get(url).json()
# 获取的图片是base64需要转换
imgdata = base64.b64decode(response.get('png'))
with open('x.png', 'wb') as f:
f.write(imgdata)
# execute 实现于Lua脚本的对接
lua = '''
function main(splash, args)
assert(splash:go('https://baidu.com'))
return {
html = splash:html(),
png = splash:png(),
har = splash:har(),
}
end
'''

url = 'http://localhost:8050/execute?lua_source=' + quote(lua)
response = requests.get(url)
print(response.text)
赏个🍗吧
0%