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执行结果。



   



         


猜你喜欢

转载自blog.csdn.net/yzp1011/article/details/80028694