1、Splash 的使用css
Splash 是一個JavaScript 渲染服務,帶有 HTTP API的輕量級瀏覽器,同時對接了 Python 中的 Twisted 和 QT 庫。利用它,一樣能夠實現動態渲染頁面的抓取。html
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
這個腳本是用 Lua 語言寫的腳本。從腳本的表面意思,它首先調用 go()方法去加載頁面,再調用 wait()方法等待必定時間,最後返回頁面的源碼、截圖和 HAR 信息。node
function main(splash, args) splash:go("http://www.baidu.com") splash:wait(0.5) local title = splash:evaljs("document.title") return {title= title} end
將代碼貼到 http://localhost:8050/ 的代碼編輯區域,點擊 Render me!按鈕 測試python
注意,在這裏定義的方法名稱叫做 main()。這個名稱必須是固定的,Splash 會默認調用這個方法。jquery
function main(splash) return { hello="world" } end
返回一個字典形式的內容。如:json
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
輸出:api
function main(splash, args) local url = args.url end
這裏第二個參數 args 就至關於 splash.args 屬性,以上代碼等價於:瀏覽器
function main(splash) local url = splash.url end
function main(splash, args) splash:go("https://www.baidu.com") splash.js_enabled = false local title = splash:evaljs("document.title") return{title= title} end
接着,從新調用 evaljs()方法執行 JavaScript 代碼,此時運行結果就會拋出異常:緩存
{ "error": 400, "info": { "line_number": 1, "error": "')' expected near char(239)", "source": "[string \"function main(splash, args)\r...\"]", "message": "[string \"function main(splash, args)\r...\"]:1: ')' expected near char(239)", "type": "LUA_INIT_ERROR" }, "description": "Error happened while executing Lua script", "type": "ScriptError" }
通常來講,不用設置此屬性,默認開啓便可。cookie
resource_timeout
此屬性能夠設置加載的超時時間,單位是秒。若是設置爲 0 或者 nil(相似 Python 中的 None ),表明不檢測超時。示例:
function main(splash) splash.resource_timeout = 0.1 assert(splash:go("https://www.taobao.com")) return splash:png() end
例,這裏將超時時間設置爲 0.1秒。若是在 0.1 秒以內沒有獲得響應,就會拋出異常。 此屬性適合在網頁加載速度較慢的狀況下設置,若是超過了某個時間無響應,則直接拋出異常並忽略便可。
images_enabled
images_enabled 屬性能夠設置圖片是否加載,默認狀況下是加載的。禁用該屬性後,能夠節省網絡流量並提升網頁加載速度。注意:禁用圖片加載可能會影響 JavaScript 渲染。禁用圖片後,外層 DOM 節點的高度會受影響,進而影響 DOM 節點的位置。所以,若是 JavaScript 對圖片節點有操做的話,其執行就會受到影響。
注意:Splash 使用了緩存。若是一開始加載出來了網頁圖片,而後禁用了圖片加載, 再從新加載頁面,以前加載好的圖片可能還會顯示出來,這時直接重啓 Splash 便可。
禁用圖片加載的示例:
function main(splash, args) splash.images_enabled = false assert(splash:go('https://www.jd.com')) return { png=splash:png()} end
這樣返回的頁面截圖就不會帶有圖片,加載速度也會快不少
plugins_enabled
plugins_enabled 屬性能夠控制瀏覽器插件(如 Flash 插件)是否開啓。默認狀況下,此屬性是 false,表示不開啓。可使用以下代碼控制其開啓和關閉:
splash。plugins_enabled = true/false
scroll_position
設置 scroll_position 屬性,能夠控制頁面上下或左右滾動。是一個比較經常使用的屬性,示例以下:
function main(splash, args) assert(splash:go('https://www.taobao.com')) splash.scroll_position = {y=400} return {png=splash:png()} end
這樣咱們就能夠控制頁面向下滾動 400 像素值。
若是要讓頁面左右滾動,能夠傳入 x 參數,代碼:
splash.scroll_position = {x=100,y=200}
Splash 對象的方法
除了前面介紹的屬性外,Splash 對象還有以下方法:
go()
go() 方法用來請求某個連接,並且它能夠模擬 GET 和 POST 請求,同時支持傳入請求頭、表單等數據,用法:
ok, reason = spalsh:go{url, baseurl=nil,headers=nil,http_method="GET",body=nil,formdata=nil}
參數說明:
url:請求的URL\
baseurl:可選參數,默認爲空,表示自願加載相對路徑。
headers:可選參數,默認爲空,表示請求頭
http_method:可選參數,默認爲空,發POST請求時的表單數據,使用Content-type 爲application/json
formdata:可選參數,默認爲空,POST的時候的表單數據,使用的Content-type爲application/x-www-form-urlencoded、
該方法的返回結果是結果 ok 和緣由 reason 的組合,若是 ok 爲空,表明網頁加載出現了錯誤,此時 reason 變量中包含了錯誤的緣由,不然證實頁面加載成功。示例:
function main(splash,args) local ok, reason = splash:go{"http://httpbin.org/post",http_method="POST",body="name=Germey"} if ok then return splash:html() end end
這裏模擬了 POST 請求,並傳入了 POST 的表單數據,若是成功,則返回頁面的源代碼。運行結果:
<html><head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">{ "args": {}, "data": "", "files": {}, "form": { "name": "Germey" }, "headers": { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Encoding": "gzip, deflate", "Accept-Language": "en,*", "Connection": "close", "Content-Length": "11", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "Origin": "null", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/602.1 (KHTML, like Gecko) splash Version/9.0 Safari/602.1" }, "json": null, "origin": "180.175.116.149", "url": "http://httpbin.org/post" } </pre></body></html>
成功實現POST請求併發送了表達數據。
wait()
wait() 方法能夠控制頁面的等待時間,使用方法:
ok, reason = splash:wait{time,cancel_on_redirect=false,cancel_on_error=true}
參數說明
time:等待的秒數
cancel_on_redirect:可選參數,默認爲false,表示若是發生了重定向就中止等待,並返回重定向結果
cancel_on_error:可選參數,默認爲false,表示若是發生了加載錯誤,就中止等待。
返回結果一樣是結果 ok 和緣由 reason 的組合。示例:
function main(splash) splash:go("https://www.taobao.com") splash:wait(2) return {html=splash:html()} end
該功能實訪問淘寶並等待2秒,隨後返回頁面源代碼的功能。
jsfunc()
jsfunc()方法能夠直接調用 JavaScript 定義的方法,但所調用的方法須要用雙中括號包圍,至關於實現了 JavaScript 方法到 Lua 腳本的轉換。示例:
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("https://www.baidu.com") return("There are %s DIVs"):format(get_div_count()) end 輸出: Splash Response: "There are 22 DIVs"
首先,聲明瞭一個 JavaScript 定義的方法,而後在頁面加載成功後調用此方法計算出頁面中 div 節點的個數。
關於 JavaScript 到 Lua 腳本的更多轉換細節,參考官方文檔: https://splash.readthedocs.io/en/ stable/scripting-ref.html#splash-jsfunc。
evaljs()
evaljs() 方法能夠執行 JavaScript 代碼並返回最後一條 JavaScript 語句的返回結果,使用方法:
result = splash:evaljs(js)
如,能夠用下面的代碼來獲取頁面標題:
local title = splash:evaljs("document.title")
runjs()
runjs() 方法能夠執行 JavaScript 代碼,與 evaljs()的功能相似,可是更偏向於執行某些動做或聲明某些方法。例如:
function main(splash, args) splash:go("https://www.baidu.com") splash:runjs("foo = function(){return 'bar'}") local result = splash:evaljs("foo()") return result end 輸出: bar
用 runjs()先聲明一個 JavaScript 定義的方法,經過 evaljs()來調用獲得的結果。
autoload()
autoload() 方法能夠設置每一個頁面訪問時自動加載的對象,使用方法:
ok, reason = splash:autoload{source_or_url, source=nil, url=nil}
參數說明:
source_or_url:JavaScript代碼或者JavaScript庫連接
source:JavaScript代碼
url:JavaScript庫連接
autoload() 方法只負責加載 JavaScript 代碼或庫,不執行任何操做。若是要執行操做,能夠調用 evaljs() 或 runjs()方法。例:
function main(splash,args) splash:autoload([[ function get_document_title(){ return document.title; } ]]) splash:go("https://www.baidu.com") return splash:evaljs("get_document_title()") end
調用 autoload()方法聲明一個 JavaScript 方法,而後經過 evaljs ()方法來執行此 JavaScript 方法。輸出百度頁面文本。
另外,也可使用 autoload()方法加載某些方法庫,如 jQuery,示例:
function main(splash, args) assert(splash:autoload("https://code.jquery.com/jquery-2.1.3.min.js")) assert(splash:go("https://www.taobao.com")) local version = splash:evaljs("$.fn.jquery") return 'JQuery version:'.. version end
輸出:"JQuery version:2.1.3"
call_later()
call_later() 方法能夠經過設置定時任務和延遲時間來實現任務延時執行,井且能夠在執行前經過 cancel () 方法從新執行定時任務。示例:
function main(splash,args) local snapshots = {} local timer = splash:call_later(function() snapshots["a"] = splash:png() splash:wait(1.0) snapshots["b"] = splash:png() end, 0.05) splash:go("https://www.taobao.com") splash:wait(3.0) return snapshots end
這裏設置了一個定時任務,0.05 秒的時候獲取網頁截圖,而後等待 1 秒,1.05 秒時再次獲取網頁截圖,訪問的頁面是淘寶,最後將截圖結果返回。 結果將是:第一次截圖時網頁尚未加載出來,第二次網頁加載成功。
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("http://httpbin.org/get") return { html = treat.as_string(response.body), url=response.url, status=response.statsus } end
輸出結果:
Splash Response:Object html:String(length 355) { "args": {}, "headers": { "Accept-Encoding": "gzip, deflate", "Accept-Language": "en,*", "Connection": "close", "Host": "httpbin.org", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/602.1 (KHTML, like Gecko) splash Version/9.0 Safari/602.1" }, "origin": "180.175.116.149", "url": "http://httpbin.org/get" }
response = splash:http_post{url,headers=nil,follow_redirects=true, body=nil}
參數說明:
url:請求URL
function main(splash, args) local treat = require("treat") local json = require("json") local response = splash:http_post{"http://httpbin.org/post", body=json.encode({name ="Germey"}), headers={["content-type"]="application/json"} } return { html=treat.as_string(response.body), url=response.url, status=response.status } end
輸出:
Splash Response:Object html:String(length 535) { "args": {}, "data": "{\"name\": \"Germey\"}", "files": {}, "form": {}, "headers": { "Accept-Encoding": "gzip, deflate", "Accept-Language": "en,*", "Connection": "close", "Content-Length": "18", "Content-Type": "application/json", "Host": "httpbin.org", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/602.1 (KHTML, like Gecko) splash Version/9.0 Safari/602.1" }, "json": { "name": "Germey" }, "origin": "180.175.116.149", "url": "http://httpbin.org/post" }
這裏成功模擬提交POST請求併發送表單數據。
set_content()
set_content() 方法用來設置頁面內容,示例:
function main(splash) assert(splash:set_content("<html><body><h1>hello</h1></body></html>")) return splash:png() end
html()
function main(splash,args) splash:go("https://httpbin.org/get") return splash:html() end 輸出:
<html><head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">{ "args": {}, "headers": { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Encoding": "gzip, deflate", "Accept-Language": "en,*", "Connection": "close", "Host": "httpbin.org", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/602.1 (KHTML, like Gecko) splash Version/9.0 Safari/602.1" }, "origin": "180.175.116.149", "url": "https://httpbin.org/get" } </pre></body></html>
png()
png() 方法用來獲取 PNG 格式的網頁截圖,示例:
function main(splash,args) splash:go("https://www.taobao.com") return splash:png() end
jpeg()
jpeg() 方法用來獲取 JPEG 格式的網頁截圖,示例:
function main(splash,args) splash:go("https://www.taobao.com") return splash:jpeg() end
har()
har() 方法用來獲取頁面加載過程描述,示例:
function main(splash, args) splash:go("https://www.baidu.com") return splash:har() end
輸出結果:
url()
url()方法能夠獲取當前正在訪問的 URL,示例:
function main(splash,args) splash:go("https://www.baidu.com") return splash:url() end 輸出: "https://www.baidu.com/"
get_cookies()
function main(splash, args) splash:go("https://www.baidu.com") return splash:get_cookies() end 輸出:
Splash Response: Array[7] 0: Object domain: ".baidu.com" expires: "2087-01-21T09:30:08Z" httpOnly: false name: "BAIDUID" path: "/" secure: false value: "70BACA9750D3C6FBE7DA3EDC2B9E0FCE:FG=1" 1: Object domain: ".baidu.com" expires: "2087-01-21T09:30:08Z" httpOnly: false name: "BIDUPSID" path: "/" secure: false value: "70BACA9750D3C6FBE7DA3EDC2B9E0FCE" 2: Object domain: ".baidu.com" expires: "2087-01-21T09:30:08Z" httpOnly: false name: "PSTM" path: "/" secure: false value: "1546496161" 3: Object domain: ".baidu.com" httpOnly: false name: "delPer" path: "/" secure: false value: "0" 4: Object domain: "www.baidu.com" httpOnly: false name: "BD_HOME" path: "/" secure: false value: "0" 5: Object domain: ".baidu.com" httpOnly: false name: "H_PS_PSSID" path: "/" secure: false value: "1456_21095_28206_28131_26350_28139_27543" 6: Object domain: "www.baidu.com" expires: "2019-01-13T06:16:01Z" httpOnly: false name: "BD_UPN" path: "/" secure: false value: "143354"
cookies = splash:add_cookie{name,value,path=nil,domain=nil,expires=nil,httpOnly=nil,secure=nil}
各個參數表明 Cookie 的各個屬性。示例:
function main(splash) splash:add_cookie{"sessionid","237465ghgfsd","/",domain="http://example.com"} splash:go("http://example.com/") return splash:html() end
function main(splash) splash:go("https://www.baidu.com") splash:clear_cookies() return splash:get_cookies() end
清除了全部的 Cookies,而後調用 get_cookies()將結果返回。 輸出:Splash Response: Array[0], Cookies 被所有清空。
get_viewport_size()
function main(splash) splash:go("https://www.baidu.com/") return splash:get_viewport_size() end
輸出:
Splash Response: Array[2]
0: 1024
1: 768
set_viewport_size()
set_viewport_size() 方法能夠設置當前瀏覽器頁面的大小,即寬高,用法:
splash:set_viewport_size(width,height)
例:訪問一個寬度自適應的頁面:
function main(splash) splash:set_viewport_size(400,700) assert(splash:go("https://cuiqingcai.com")) return splash:png() end
set_viewport_full()
set_viewport_full() 方法能夠設置瀏覽器全屏顯示,示例:
function main(splash) splash:set_viewport_full() assert(splash:go("https://www.cuiqingcai.com")) return splash:png() end
set_user_agent()
set_user_agent() 方法能夠設置瀏覽器的 User-Agent,示例:
function main(splash) splash:set_user_agent('splash') splash:go("http://httpbin.org/get") return splash:html() end
這裏將瀏覽器的 User-Agent 設置爲 Splash. 結果:User-agent 被設置成功。
set_custom_headers()
set_custom_headers() 方法能夠設置請求頭,示例:
function main(splash) splash:set_custom_headers({ ["User-Agent"] = "Splash", ["Site"] = "Splash", }) splash:go("http://httpbin.org/get") return splash:html() end
這裏設置了請求頭中的 User-Agent 和 Site 屬性,輸出結果中User-Agent 和Site被更改。
select()
select()方法能夠選中符合條件的第一個節點,若是有多個節點符合條件只會返回一個,其參數是 css 選擇器。示例:
function main(splash) splash:go("https://www.baidu.com") input = splash:select("#kw") input:send_text('Splash') splash:wait(3) return splash:png() end
首先訪問了百度,選中了搜索框,隨後調用 send_text()方法填寫了文本,而後返回網頁截圖。成功填寫了輸入框。
select_all()
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
這裏經過 CSS 選擇器選中節點的正文內容,隨後遍歷了全部節點,將其中的文本獲取下來。
mouse_click()
mouse_click() 方法能夠模擬鼠標點擊操做,傳入的參數爲座標值 X 和 Y。也能夠直接選中某個節點,調用此方法,示例:
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
首先選中頁面的輸入框,輸入文本,而後選中「提交」按鈕,調用了 mouse_click() 方法提交查詢,而後頁面等待3秒,返回截圖。成功獲取了查詢後的頁面內容,模擬了百度搜索操做。
Splash 對象的全部 API 操做:詳細說明參見官方文檔 https://splash.readthedocs.io/en/stable/scripting-ref.html 。
針對頁面元素 API 操做:https://splash.readthedocs.io/en/stable/scripting-element-object.html。
curl http://localhost:8050/render.html?url=https://www.baidu.com
給此接口傳遞了一個 url 參數來指定渲染的 URL,返回結果即頁面渲染後的源代碼。用 Python 實現代碼:
import requests url = 'http://localhost:8050/render.html?url=https://www.baidu.com' response = requests.get(url) print(response.text)
這樣就能夠成功輸出 百度頁面渲染後的源代碼了。
import requests url = 'http://localhost:8050/render.html?url=https://www.taobao.com&wait=5' response = requests.get(url) print(response.text)
此時獲得響應的時間就會變長,如這裏會等待 5 秒多鍾才能獲取淘寶頁面的源代碼。
curl http://localhost:8050/render.png?url=https://www.taobao.com&wait=5&width=1000&height=700
這裏傳入 width 和 height 來設置頁面大小爲 1000 像素×700 像素。
import requests url = 'http://localhost:8050/render.png?url=https://www.jd.com&wait=5&width=1000&height=700' response = requests.get(url) with open('taobao.png','wb') as f: f.write(response.content)
這樣就成功獲取了京東首頁渲染完成後的頁面截圖,詳細的參數設置參考官網文檔: https://splash.readthedocs.io/en/stable/api.html#render-png。
curl http://localhost:8050/render.har?url=https://www.jd.com&wait=5
運回結果很是多,是一個 JSON 格式的數據,其中包含頁面加載過程當中的 HAR 數據
curl http://localhost:8050/render.json?url=https://httpbin.org
能夠經過傳人不一樣參數控制其返回結果。如:傳人 html=1,返回結果即會增長源代碼數據;傳入 png=1 ,返回結果即會增長頁面 PNG 截圖數據;傳入 har=1 ,則會得到頁面 HAR 數據。例:
curl http://localhost:8050/render.json?url=https://httpbin.org&html=1
這樣返回的 JSON 結果會包含網頁源代碼和 HAR 數據。
更多參數設置參考官方文檔: https://splash.readthedocs.io/en/stable/api.html#render-json。
excute
excute() 接口是最爲強大的接口。前面說了不少 Splash Lua 腳本的操做,用此接口即可實現與 Lua 本的對接。 前面的 render.html 和 render.png 等接口對於通常的 JavaScript 渲染頁面是足夠了,可是若是要實現一些交互操做的話,它們仍是無能爲力,這裏就須要使用 execute 接口。 先實現一個最簡單的腳本,直接返回數據:
function main(splash) return 'hello' end
而後將腳本轉化爲 URL 編碼後的字符串,拼接到 execute 接口後面,示例:
curl http://localhost:8050/execute?lua_source=function+main%28splash%29%0D%0A++return+%27hello%27%0D%0Aend
經過 lua_source 參數傳遞了轉碼後的 Lua 腳本,經過 execute 接口獲取了最終腳本的執行結果。 用 Python 實現代碼:
import requests from urllib.parse import quote lua = ''' function main(splash) return 'hello' end ''' url = 'http://localhost:8050/execute?lua_source=' + quote(lua) response = requests.get(url) print(response.text) 輸出: hello
這裏用 Python 中的三引號將 Lua 腳本包括起來,而後用 urllib.parse 模塊裏的 quote()方法將腳本進行 URL 轉碼,隨後構造 Splash 請求 URL,將其做爲 lua_source 參數傳遞,這樣運行結果就會顯示 Lua 腳本執行後的結果。
實例:
import requests from urllib.parse import quote lua = ''' function main(splash,args) local treat = require("treat") local response = splash:http_get("http://httpbin.org/get") return { html = treat.as_string(response.body), url=response.url, status=response.status } end ''' url = 'http://localhost:8050/execute?lua_source='+quote(lua) #不要忘記後面的‘=’ response = requests.get(url) print(response.text)
輸出:
{"html": "{\n \"args\": {}, \n \"headers\": {\n \"Accept-Encoding\": \"gzip, deflate\", \n \"Accept-Language\": \"en,*\", \n \"Connection\": \"close\", \n \"Host\": \"httpbin.org\", \n \"User-Agent\": \"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/602.1 (KHTML, like Gecko) splash Version/9.0 Safari/602.1\"\n }, \n \"origin\": \"180.165.241.94\", \n \"url\": \"http://httpbin.org/get\"\n}\n", "status": 200, "url": "http://httpbin.org/get"}
返回結果是 JSON 形式,成功獲取了請求的 URL, 狀態碼和網頁源代碼。
以前所說 Lua 腳本都可以用此方式與 Python 進行對接,全部網頁的動態渲染,模擬點擊、表單提交、頁面滑動、延時等待後的一些結果都可以自由控制,獲取頁面源碼和截圖也都ok。