入口及返回值
function main (splash,args)
splash:go("http://www.baidu.com")
splash:wait(0.5)
local title = splash:evaljs("document.title")
return {title=title}
end
Splash支持異步處理激挪,不顯示指明回調(diào)方法,回調(diào)的跳轉(zhuǎn)在Splash內(nèi)部完成嗡综,wait()相當(dāng)于sleep(),png()返回截圖
function main(splash, args)
local example_urls = {"www.baidu.com","www.taobao.com","www.zhihu.com"}
local urls = args.urls or example_urls
local results = {}
for index,url in ipairs(urls) do
local ok,reason = splash:go("http://"..url)
if ok then
splash:wait(2)
results[url] = splash:png()
end
end
return results
end
Splash對(duì)象屬性
mai()方法的第一個(gè)參數(shù):splash對(duì)象(它的屬性和方法控制加載過程)
args:獲取加載時(shí)配置的參數(shù),如URL,GET請(qǐng)求:可獲取GET請(qǐng)求參數(shù)碎节,POST請(qǐng)求:獲取表單提交的數(shù)據(jù)
function main(splash, args)
local url = args.url
return url
end
與下面等價(jià)
function main(splash)
local url = splash.args.url
return url
end
js_enabled:JavaScript執(zhí)行開關(guān),默認(rèn)為true
resource_timeout:設(shè)置加載的超時(shí)時(shí)間(秒)抵卫,0/nil表示不檢測超時(shí)
image_enabled:設(shè)置圖片是否加載狮荔,默認(rèn)true加載,禁用會(huì)影響JavaScript的渲染(影響DOM節(jié)點(diǎn)的位置)介粘,Splash使用緩存殖氏,一開始加載圖片,后面禁用仍會(huì)出現(xiàn)圖片姻采,重啟Splash即可
plugins_enabled:瀏覽器插件是否開啟雅采,默認(rèn)false
scroll_position:控制頁面上下/左右滾動(dòng)
function main(splash, args)
splash:go(args.url)
splash.scroll_position = {x=100,y=400}
return {png=splash:png()}
end
go():請(qǐng)求某個(gè)鏈接,可傳入請(qǐng)求頭慨亲、表單等數(shù)據(jù)
jsfunc():直接調(diào)用JavaScript定義的方法婚瓜,該方法使用雙中括號(hào)括起
function main(splash, args)
local get_div_count = splash:jsfunc([[
function(){
var body = document.body;
var divs = body.getElementsByTagName('div');
return divs.length;
}
]])
splash:go(args.url)
return ("There are %s divs"):format(get_div_count())
end
evaljs():執(zhí)行JavaScript代碼后返回最后一條JavaScript語句返回的結(jié)果
result = splash:evaljs(js)
runjs():聲明JavaScript定義的語句塊,通過evaljs()調(diào)用
function main(splash, args)
splash:go(args.url)
splash:runjs("foo = function(){return 'hello'}")
local result = splash:evaljs("foo()")
return result
end
autoload():設(shè)置每個(gè)頁面訪問時(shí)自動(dòng)加載的對(duì)象刑棵,可以是JavaScript代碼或庫巴刻,但是不執(zhí)行操作,執(zhí)行調(diào)用evaljs()
ok,reason = splash:autoload{source_or_url}
call_later():設(shè)置定時(shí)任務(wù)和延時(shí)執(zhí)行,如下在go訪問后使用wait(3.0)等待3秒才返回所有截圖蛉签,期間設(shè)置0.2秒獲取截圖胡陪,中間等待1秒沥寥,1.2秒的時(shí)候再次獲取截圖,最后3秒后返回所有截圖
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(args.url)
splash:wait(3.0)
return snapshots
end
http_get():模擬發(fā)送http的get請(qǐng)求
response = splash:http_get(url,headers=nil,follow_redirects=true)
follow_redirects表示是否啟動(dòng)自動(dòng)重定向柠座,默認(rèn)true
請(qǐng)求的地址:http://httpbin.org/get
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
http_post:發(fā)送post請(qǐng)求邑雅,需要body參數(shù)
請(qǐng)求的地址:http://httpbin.org/post
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():設(shè)置頁面內(nèi)容
html():返回獲取的頁面源碼信息
result = splash:html()
png():返回PNG格式的截圖
jpeg():返回JPEG格式的截圖
png = splash:png()
jpeg = splash:jpeg()
har():獲取頁面加載過程描述
url():獲取當(dāng)前URL
get_cookies():獲取當(dāng)前訪問鏈接的cookies
add_cookies():添加cookies
clear_cookies():清除cookies
get_viewport_size():獲取當(dāng)前瀏覽器的大小
set_viewport_size():設(shè)置當(dāng)前瀏覽器的大小
set_viewport_full():設(shè)置瀏覽器全屏顯示
set_user_agent():設(shè)置瀏覽器的user_agent
set_custom_headers():設(shè)置請(qǐng)求頭信息
select():返回選中符合條件的第一個(gè)節(jié)點(diǎn),參數(shù)是CSS選擇器
select_all():返回選中符合條件的所有節(jié)點(diǎn)妈经,參數(shù)是CSS選擇器
mouse_click():模擬鼠標(biāo)點(diǎn)擊事件
參考鏈接:
1蒂阱、http://splash.readthedocs.io/en/stable/scripting-ref.html#attributes
2、http://splash.readthedocs.io/en/stable/scripting-overview.html