splash3.2學習筆記——HTTP API(2)
render.har
使用HAR格式返回Splash與網站互動的資訊。包含請求、響應、時間線、頭部資訊,等。
可以使用HAR Viewer顯示端點返回的視覺化資訊;與FIrefox和Chrome開發者工具中的"Network"標籤。
當前,該端點不顯示原請求內容;只有像頭部和時間線的meta-information可用,當response-body選項設定為1時,包含響應內容。
response_body:整型,可選
可能值為0和1,當response_body為1時,響應內容包含在HAR記錄中。預設值為0.
render.json
返回javascript渲染後頁面的json編碼字典。可以按照引數傳遞,包含HTML,PNG和其他資訊。
引數:
與render.jpeg相同,外加以下引數:
html:整型,可選
是否在輸出中包含HTML。可能值為0(不包含)或1(包含),預設為0
png:整型,可選
是否在輸出中包含PNG。可能值為1(包含)和0(不包含),預設為0.
jpeg:整型,可選
是否在輸出中包含JPEG。可能值為1(包含)和0(不包含),預設為0。
iframes:整型,可選
是否在輸出中包含子框架資訊。可能值為1(包含)和0(不包含),預設為0。
script:整型,可選
是否在輸出中包含javascript的final段執行結果。可能值為1(包含)和0(不包含),預設為0.
console:整型,可選
是否在輸出中包含控制檯輸出資訊。可能值為1(包含)和0(不包含),預設為0.
history:整型,可選
是否在輸出中包含網頁子框架請求/響應的歷史。可能值為1(包含)和0(不包含),預設為0.
用該選項獲取HTTP狀態碼和頭部。只有主體請求/響應資訊會被返回(如像影象這樣的相關資源和AJAX查詢資訊將不返回),使用har引數,獲取所有請求和響應資訊。
har:整型,可選
在輸出中是否包含HAR。可能值為1(包含)和0(不包含),預設為0.如果該選項開啟,結果將包含與render.har通過'har'鍵獲得的資料相同。
預設狀態下,響應正文不被包含。使用'response_body'選項,可以開啟。
response_body:整型,可選
可能值為1和0.當response_body=1,響應正文包含在HAR記錄中。預設response_body=0,當'har'和'history'均為0時,該選項無效。
例項:
預設狀況下,URL,請求URL,頁面標題和框架模型被返回:
{
"url": "http://crawlera.com/",
"geometry": [0, 0, 640, 480],
"requestedUrl": "http://crawlera.com/",
"title": "Crawlera"
}
在請求中新增'html=1',將HTML加入結果:
{
"url": "http://crawlera.com/",
"geometry": [0, 0, 640, 480],
"requestedUrl": "http://crawlera.com/",
"html": "<!DOCTYPE html><!--[if IE 8]>....",
"title": "Crawlera"
}
在結果中新增base64編碼的PNG截圖新增到結果中:
{
"url": "http://crawlera.com/",
"geometry": [0, 0, 640, 480],
"requestedUrl": "http://crawlera.com/",
"png": "iVBORw0KGgoAAAAN...",
"title": "Crawlera"
}
新增'iframes=1',浮動框架資訊將被新增到結果中:
{
"geometry": [0, 0, 640, 480],
"frameName": "",
"title": "Scrapinghub | Autoscraping",
"url": "http://scrapinghub.com/autoscraping.html",
"childFrames": [
{
"title": "Tutorial: Scrapinghub's autoscraping tool - YouTube",
"url": "",
"geometry": [235, 502, 497, 310],
"frameName": "<!--framePath //<!--frame0-->-->",
"requestedUrl": "http://www.youtube.com/embed/lSJvVqDLOOs?version=3&rel=1&fs=1&showsearch=0&showinfo=1&iv_load_policy=1&wmode=transparent",
"childFrames": []
}
],
"requestedUrl": "http://scrapinghub.com/autoscraping.html"
}
注意到,iframes可以巢狀
Curl例項:
# full information
curl 'http://localhost:8050/render.json?url=http://domain.com/page-with-iframes.html&png=1&html=1&iframes=1'
# HTML and meta information of page itself and all its iframes
curl 'http://localhost:8050/render.json?url=http://domain.com/page-with-iframes.html&html=1&iframes=1'
# only meta information (like page/iframes titles and urls)
curl 'http://localhost:8050/render.json?url=http://domain.com/page-with-iframes.html&iframes=1'
# render html and 320x240 thumbnail at once; do not return info about iframes
curl 'http://localhost:8050/render.json?url=http://domain.com/page-with-iframes.html&html=1&png=1&width=320&height=240'
# Render page and execute simple Javascript function, display the js output
curl -X POST -H 'content-type: application/javascript' \
-d 'function getAd(x){ return x; } getAd("abc");' \
'http://localhost:8050/render.json?url=http://domain.com&script=1'
# Render page and execute simple Javascript function, display the js output and the console output
curl -X POST -H 'content-type: application/javascript' \
-d 'function getAd(x){ return x; }; console.log("some log"); console.log("another log"); getAd("abc");' \
'http://localhost:8050/render.json?url=http://domain.com&script=1&console=1'
execute
執行自定義渲染指令碼,返回結果。
render.html,render.png,render,jpeg,render.har和render.json端點可以滿足很多通常使用情形。但很多時候還是不夠的。這個端點允許書寫自定義Splash指令碼。
引數:
lua_source:字串,必需
瀏覽器自動化指令碼。
timeout:浮點數,可選
與render.html中timeout引數相同
allowed_domains、proxy、filters、save_args、load_args引數均與render.html中相同。
你可以傳遞任何其他引數,所有在指令碼中可以傳遞給excute端點的引數都在splash.args表中。
run
該端點與excute相同,但在function main(splash, args) ... end
中自動包含lua_source。例如,如果傳送指令碼到execute:
function main(splash, args)
assert(splash:go(args.url))
assert(splash:wait(1.0))
return splash:html()
end
在run端點中等同的指令碼是:
assert(splash:go(args.url))
assert(splash:wait(1.0))
return splash:html()
在頁面上下文中執行自定義javascript程式碼:
Splash支援頁面環境中執行javascript程式碼。javascript程式碼在頁面結束載入後,頁面渲染前執行。允許使用javascript程式碼修改正在渲染的頁面。
使用js_source引數執行javascript程式碼。包含將被執行的javascript程式碼。
注意瀏覽器和代理可使用GET傳送的資料量限制,所以使用content-type: application/json
來POST請求是個好辦法。
Curl例項:
curl -X POST -H 'content-type: application/json' \
-d '{"js_source": "document.title=\"My Title\";", "url": "http://example.com"}' \
'http://localhost:8050/render.html'
另一種同樣作用的方法是使用content-type為'application/javascript’的POST請求。請求體應包含要被執行的程式碼。
Curl例項:
# Render page and modify its title dynamically
curl -X POST -H 'content-type: application/javascript' \
-d 'document.title="My Title";' \
'http://localhost:8050/render.html?url=http://domain.com'
使用帶有script=1引數的render.json端點,獲取頁面上下文javascript執行結果。