table of Contents
- splash object properties
- splash object's methods
- go()
- wait()
- evaljs ()
- Runji ()
- autoload()
- call_later()
- http_get()
- http_post()
- set_content()
- html()
- png()
- jpeg()
- is ()
- url()
- get_cookies()
- add_cookies()
- clear_cookies()
- get_viewport_size()
- set_viewport_size()
- set_viewport_full()
- set_user_agent()
- set_custom_headers()
- select()
- select_all()
- mouse_click()
splash object properties
args
Mai first parameter () method: Splash object (its properties and methods for controlling the loading process)
args: loading time of access parameters, such as the URL, the GET request: GET request is available parameters, POST request: Get form submission data
function main(splash, args)
local url = args.url
return url
end
And is equivalent to the following
function main(splash)
local url = splash.args.url
return url
end
js_enabled
JavaScript execution switch, the default is true
Disable JS:
function main (splash,args)
splash:go("http://www.baidu.com")
splash.js_enabled = false
local title = splash:evaljs("document.title")
return {title=title}
end
resource_timeout
Set timeout load (s), 0 / nil indicates no detection timeout
image_enabled
Set whether the image is loaded, loaded default true, will affect disabled JavaScript rendering (affect the position of DOM node), Splash cache is used to start loading a picture, the picture will still be behind disabled, you can restart Splash
plugins_enabled
Browser plug-in is turned on by default false
scroll_position
Control page up and down / left and right scroll (x = 100 (scroll left and right), y = 100 (scroll))
Scroll down 400 pixels:
function main (splash,args)
assert(splash:go("http://www.taobao.com"))
splash.scroll_position = {y = 400}
return {ping = splash:png()}
end
splash object's methods
go()
Request a link, you can head incoming requests, forms and other data
ok,reason = splash:go{url, baseurl=nil, headers=nil, http_method="GET", body=nil , formdata=nil}
url: url requests
baseurl: optional parameter, the default is null, indicating the relative path resources, and load
headers: an optional parameter, default is empty, indicating that the request header
http_method: optional parameter, defaults to GET
body: an optional parameter, default is empty, POST of form data when using the content-type of application / json
formdata: optional parameter, default is empty, POST of form data when using the content-type is application / x-www-form-urlencoded
wait()
Control page latency
ok, reason = splash:wait{time, cancel_on_redirect=false, cancel_on_error=true}
time: waiting time
cancel_on_error: optional parameter, default false, indicates that an error loading occurs, stop waiting
cancel_on_redirect: optional parameter, default false, indicating that redirection occurs to stop waiting and returns a redirect result
evaljs ()
After executing JavaScript code returns the results of the last JavaScript statement returns
result = splash:evaljs(js)
Runji ()
JavaScript declaration statement block defined by evaljs () call
function main(splash, args)
splash:go(args.url)
splash:runjs("foo = function(){return 'hello'}")
local result = splash:evaljs("foo()")
return result
end
autoload()
The settings on each page object is automatically loaded when accessed, it can be a JavaScript code or libraries, but no action, calling evaljs ()
ok,reason = splash:autoload{source_or_url}
source_or_url: js code or js library links
source: js Code
url: js library links
call_later()
And set the timer to delay execution task, as used in the go access wait (3.0) wait 3 seconds before returning all the shots, obtaining theme provided 0.2 seconds during the intermediate waiting for one second, 1.2 seconds when the shots acquired again, after the last 3 seconds return all screenshots
function main(splash, args)
local snapshots = {}
local timer = splash:call_later(function()
snapshots["a"]=splash:png()
splash:wait(1)
snapshots["b"]=splash:png()
end,0.2)
splash:go("https://www.taobao.com")
splash:wait(3.0)
return snapshots
end
http_get()
get simulated send http request
response = splash:http_get(url,headers=nil,follow_redirects=true)
function main(splash, args)
local treat = require("treat")
local response = splash:http_get(args.url)
return {
html = treat.as_string(response.body),
url = response.url,
status = response.status
}
end
url: url requests
headers: an optional parameter, default is empty, the request header
follow_redirects: optional parameter that indicates whether to start automatic redirection, default true
http_post()
Sending post request body parameters need
response = splash:http_post{url, headers =「1il, follow_redirects=true, body=nil}
url: url requests
headers: an optional parameter, default is empty, the request header
follow_redirects: optional parameter that indicates whether to start automatic redirection, default true
body: optional parameter, the form parameters, the default is empty
function main(splash, args)
local treat = require("treat")
local json = require("json")
local response = splash:http_post{args.url,
body=json.encode({name="aha"}),
headers={["content-type"]="application/json"}
}
return {
html = treat.as_string(response.body),
url = response.url,
status = response.status
}
end
set_content()
Settings page content
function main(splash)
assert(splash:set_content("<html><body><hl>hello</hl></body></html>"))
return splash: png ()
end
html()
Back to the source information obtained
function main(splash, args)
splash:go("https://httpbin.org/get")
return splash: html()
end
png()
Screenshot return to PNG format
png = splash:png()
function main(splash, args)
splash:go("https:/taobao.com")
return splash: png()
end
jpeg()
Return screenshot in JPEG format
jpeg = splash:jpeg()
function main(splash, args)
splash:go("https:/taobao.com")
return splash: jpeg()
end
is ()
Get the page loading process description
function main(splash, args)
splash:go("https:/taobao.com")
return splash: har()
end
url()
Get the current URL
function main(splash, args)
splash:go("https:/taobao.com")
return splash: url()
end
get_cookies()
Get cookies current access link
function main(splash, args)
splash:go("https:/taobao.com")
return splash: get_cookies()
end
add_cookies()
Add cookies
cookies = splash:add_cookie{name, value, path=nil, domain=nil, expires=nil, httpOnly=nil, secure=nil}
function main(splash)
splash:add_cookie{"sessionid","23746Sghgfsd ","/", domain="http://example.com"}
splash:go("http://example.com/")
return splash:html()
end
clear_cookies()
Clear cookies
function main(splash, args)
splash:go("https:/taobao.com")
splash:clear_cookies()
return splash: get_cookies()
end
get_viewport_size()
Gets the size of the current browser
function main(splash, args)
splash:go("https:/taobao.com")
return splash:get_viewport_size()
end
set_viewport_size()
Set the size of the current browser
function main(splash, args)
splash:set_viewport_size(1200,800)
assert(splash:go("https:/taobao.com"))
return splash:png()
end
set_viewport_full()
Set your browser to full screen
function main(splash, args)
splash:set_viewport_full()
assert(splash:go("https:/taobao.com"))
return splash:png()
end
set_user_agent()
Set your browser user_agent
function main(splash, args)
splash:set_user_agent('Splash')
assert(splash:go("http://httpbin.org/get"))
return splash:html()
end
set_custom_headers()
Header information setting request
function main(splash, args)
splash:set_custom_headers({
["User-Agent"] = "Splash",
["Sitle"] = "Splash",
})
assert(splash:go("http://httpbin.org/get"))
return splash:html()
end
select()
The first node returns selected qualified, the parameters are CSS selectors
function main(splash, args)
splash:go("https://www.baidu.com/")
input = splash:select("#kw")
input:send_text("Splash")
splash:wait(3)
return splash:png()
end
select_all()
Returns the selected nodes meet the conditions of all the parameters are CSS selectors
function main(splash)
local treat = require('treat')
assert(splash:go("http://quotes.toscrape.com"))
assert(splash:wait(0.5))
local texts = splash:select_all('.quote .text')
local results = {}
for index,text in ipairs(texts) do
results[index] = text.node.innerHTML
end
return treat.as_array(results)
end
mouse_click()
Simulated mouse click events
function main(splash)
splash:go("https://www.baidu.com")
input = splash:select("#kw")
input:send_text('Splash')
submit = splash:select('#su')
submit:mouse_click()
splash:wait(3)
return splash:png()
end