Splash的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_1290259791/article/details/82975386

Splash

Splash是一个JavaScript渲染服务,是一个带有HTTP API的轻量级浏览器,同时它对接了Python中的Twisted和QT库。利用它,我们可以实现动态渲染页面的抓取。

主要说一下Splash的简单属性和方法。

对象属性

1、args

获取加载时配置的参数

2、js_enabled

是Splash的JavaScript执行开关,默认True开启。

3、plugins_enable

控制浏览器插件(如Flash插件)是否开启,默认False不开启。

4、images_enable

设置图片是否价值,默认是加载。

5、resource_timeout

设置加载的超时时间,单位秒,设置为0,代表不检测。
该属性适合在网页加载速度较慢的情况下设置,超过某个时间就直接抛出异常。

6、scroll_position

控制页面的上下或左右滚动。

function main(splash, args)
  assert(splash:go(args.url))
  splash.scroll_position = {x=100,y=400}
  return {
    png = splash:png(),
  }
end

对象方法

1、go()

请求某个链接,模拟Get和Post请求,支持传入请求头、表单等数据
ok,reason=splash.go{url,baseurl=nil,headers=nil,http_method="GET",body=nil,formdata=nil}

  • url:请求的url
  • baseurl:可选,默认空,表示资源加载相对路径
  • headers:可选,默认空,表示请求头
  • http_method:可选,默认为GET,支持Post
  • body:可选,默认空,发POST请求的表单数据,使用的Content-Typeapplication/json
  • formdata:可选,默认空,发POST请求的表单数据,使用Content-Typeapplication/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=Hubo"}
  if ok then
        return splash:html()
  end
end

2、wait()

控制页面的等待时间
ok, reason = splash:wait{time,cancel_on_redirect=false,cancel_on_error=true}

  • time:等待的秒数
  • cancel_onredirect:可选,默认false,表示发生重定向就停止等待,并返回重定向结果
  • cancel_on_error:可选,默认false,表示发生了加载错误就停止等待。
function main(splash, args)
  assert(splash:go("http://taobao.com"))
  splash:wait(5)
  return {
    html = splash:html()
  }
end

3、evaljs()

执行JavaScript代码返回最后一条JS语句返回的结果。

local title = splash:evaljs("document:title")

4、http_get()

模拟发送HTTP的GET请求
response = splash:http_get{url, headers=nil, follow_redirects=true}

  • url:请求URL
  • headers:可选,默认空,请求头
  • follow_redirects:可选,是否启动自动重定向
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,
    statud = response.status
  }
end

5、http_post()

模拟发送POST请求,相对GET请求多了一个body/
response = splash:http_post{url, headers=nil, follow_redirects=true, body=nil}

  • url:请求URL
  • headers:可选,默认空,请求头
  • follow_redirects:可选,是否启动自动重定向
  • body:可选,默认空

6、set_content()

设置页面内容

function main(splash)
  assert(splash:set_content("<h1>Hello</h1>"))
  return {
    splash:png()
  }

end

7、html()

获取网页的源代码

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

8、png()

获取PNG格式的网页截图

function main(splash)
  splash:go("https://httpbin.org/get")
  return {
    splash:png()
  }
end

9、jpeg()

获取JPEG格式的网页截图

10、har()

获取页面的加载过程

function main(splash)
  splash:go("https://www.baidu.com")
  return {
    splash:har()
  }
end

11、url()

获取当前正在访问的url

12、get_cookies()

获取当前页面的Cookies

function main(splash)
  splash:go("https://www.baidu.com")
  return {
    splash:get_cookies()
  }
end

13、add_cookies()

为当前页面添加Cookies
cookies = splash:add_cookies{name,value,path=nil,domain=nil}

14、clear_cookies()

清除所有的Cookies

15、get_viewport_size()

获取当前浏览器页面大小

16、set_viewport_seze()

设置当前浏览器页面大小

17、set_viewport_full()

设置浏览器全屏显示

18、set_user_agent()

设置浏览器的User-Agent

function main(splash)
  splash:set_user_agent('Splash')
  splash:go("http://httpbin.org/get")
  return {
    splash:html()
  }
end

19、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

猜你喜欢

转载自blog.csdn.net/qq_1290259791/article/details/82975386