splash對象的屬性和方法


splash對象屬性

args

mai()方法的第一個參數:splash對象(它的屬性和方法控制加載過程)
args:獲取加載時配置的參數,如URL,GET請求:可獲取GET請求參數,POST請求:獲取表單提交的數據

function main(splash, args)
    local url = args.url
  return url
end

與下面等價

function main(splash)
    local url = splash.args.url
  return url
end

js_enabled

JavaScript執行開關,默認為true

禁用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

設置加載的超時時間(秒),0/nil表示不檢測超時

image_enabled

設置圖片是否加載,默認true加載,禁用會影響JavaScript的渲染(影響DOM節點的位置),Splash使用緩存,一開始加載圖片,后面禁用仍會出現圖片,重啟Splash即可

plugins_enabled

瀏覽器插件是否開啟,默認false

scroll_position

控制頁面上下/左右滾動(x = 100(左右滾動),y = 100(上下滾動))

向下滾動400像素:

function main (splash,args)
  assert(splash:go("http://www.taobao.com"))
  splash.scroll_position = {y = 400}
  return {ping = splash:png()}
end

splash對象的方法

go()

請求某個鏈接,可傳入請求頭、表單等數據

ok,reason = splash:go{url, baseurl=nil, headers=nil, http_method="GET", body=nil , formdata=nil}

url:請求的url

baseurl:可選參數,默認為空,表示資源和加載相對路徑

headers:可選參數,默認為空,表示請求頭

http_method:可選參數,默認為GET

body:可選參數,默認為空,POST的時候的表單數據,使用的content-type為application/json

formdata:可選參數,默認為空,POST的時候的表單數據,使用的content-type為application/x-www-form-urlencoded

wait()

控制頁面等待時間

ok, reason = splash:wait{time, cancel_on_redirect=false, cancel_on_error=true}

time:等待的時間

cancel_on_error:可選參數,默認false,表示發生錯誤加載,就停止等待

cancel_on_redirect:可選參數,默認false,表示發生重定向就停止等待,並返回重定向結果

evaljs()

執行JavaScript代碼后返回最后一條JavaScript語句返回的結果

result = splash:evaljs(js)

runjs()

聲明JavaScript定義的語句塊,通過evaljs()調用

function main(splash, args)
  splash:go(args.url)
  splash:runjs("foo = function(){return 'hello'}")
  local result = splash:evaljs("foo()")
  return result
end

autoload()

設置每個頁面訪問時自動加載的對象,可以是JavaScript代碼或庫,但是不執行操作,執行調用evaljs()

ok,reason = splash:autoload{source_or_url}

source_or_url:js代碼或js庫鏈接

source:js代碼

url:js庫鏈接

call_later()

設置定時任務和延時執行,如下在go訪問后使用wait(3.0)等待3秒才返回所有截圖,期間設置0.2秒獲取截圖,中間等待1秒,1.2秒的時候再次獲取截圖,最后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("https://www.taobao.com")
  splash:wait(3.0)
  return snapshots
end

http_get()

模擬發送http的get請求

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

headers:可選參數,默認為空,請求頭

follow_redirects:可選參數,表示是否啟動自動重定向,默認true

http_post()

發送post請求,需要body參數

response = splash:http_post{url, headers =「1il, follow_redirects=true, body=nil}

url:請求的url

headers:可選參數,默認為空,請求頭

follow_redirects:可選參數,表示是否啟動自動重定向,默認true

body:可選參數,即表單參數,默認為空

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()

設置頁面內容

function main(splash) 
    assert(splash:set_content("<html><body><hl>hello</hl></body></html>")) 
    return splash: png ()
end 

html()

返回獲取的頁面源碼信息

function main(splash, args) 
splash:go("https://httpbin.org/get") 
return splash: html() 
end

png()

返回PNG格式的截圖

png = splash:png()
function main(splash, args) 
splash:go("https:/taobao.com") 
return splash: png() 
end

jpeg()

返回JPEG格式的截圖

jpeg = splash:jpeg()
function main(splash, args) 
    splash:go("https:/taobao.com") 
    return splash: jpeg() 
end

har()

獲取頁面加載過程描述

function main(splash, args) 
    splash:go("https:/taobao.com") 
    return splash: har() 
end

url()

獲取當前URL

function main(splash, args) 
	splash:go("https:/taobao.com") 
	return splash: url() 
 end

get_cookies()

獲取當前訪問鏈接的cookies

function main(splash, args) 
	splash:go("https:/taobao.com") 
	return splash: get_cookies() 
 end

add_cookies()

添加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()

清除cookies

function main(splash, args) 
	splash:go("https:/taobao.com") 
  splash:clear_cookies()
	return splash: get_cookies() 
 end

get_viewport_size()

獲取當前瀏覽器的大小

function main(splash, args) 
	splash:go("https:/taobao.com") 
	return splash:get_viewport_size()
 end

set_viewport_size()

設置當前瀏覽器的大小

function main(splash, args) 
  splash:set_viewport_size(1200,800)
  assert(splash:go("https:/taobao.com"))
  return splash:png()
 end

set_viewport_full()

設置瀏覽器全屏顯示

function main(splash, args) 
	splash:set_viewport_full()
  assert(splash:go("https:/taobao.com"))
  return splash:png()
 end

set_user_agent()

設置瀏覽器的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()

設置請求頭信息

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()

返回選中符合條件的第一個節點,參數是CSS選擇器

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()

返回選中符合條件的所有節點,參數是CSS選擇器

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()

模擬鼠標點擊事件

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM