Splash基本使用

Splash_基本使用

image-20220728163156764

Splash对象属性

上图中main()方法的第一个参数是splash,这个对象非常重要,它类似于Selenium中的WebDriver对象

scroll_position

控制页面上下或左右滚动

splash.scroll_position = {x=100, y=200}

Splash对象的方法

go()

该方法用来请求某个链接,而且它可以模拟GET和POST请求,同时支持传入请求头、表单等数据

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

返回结果是结果ok和原因reason

如果ok为空,代表网页加载出现了错误,此时reason变量中包含了错误的原因

参数含义
url请求的URL
baseurl可选参数,默认为空,表示资源加载相对路径
headers可选参数,默认为空,表示请求头
http_method可选参数,默认为GET,同时支持POST
body可选参数,默认为空,发POST请求时的表单数据,使用的Content-type为application/json
formdata可选参数,默认为空,POST的时候的表单数据,使用的Content-type为application/x-www-form-urlencoded
splash:go{"http://www.sxt.cn", http_method="POST", 
body="name=17703181473"}

wait()

控制页面的等待时间

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

参数含义
time等待的秒数
cancel_on_redirect可选参数,默认为false,表示如果发生了重定向就停止等待,并返回重定向结果
cancel_on_error可选参数,默认为false,表示如果发生了加载错误,就停止等待
function main(splash)
   splash:go("https://www.taobao.com")
   splash:wait(2)
   return {html=splash:html()}
end

jsfunc()

直接调用JavaScript定义的方法,但是所调用的方法需要用双中括号包围,这相当于实现了JavaScript方法到Lua脚本的转换

function main(splash, args)
 splash:go("http://www.sxt.cn")
 local scroll_to = splash:jsfunc("window.scrollTo")
 scroll_to(0, 300)
 return {png=splash:png()}
end

function main(splash, args)
 local get_div_count = splash:jsfunc([[
 function () {
  var divs = document.getElementsByClassName("course_p")[0].innerHTML;
  return divs;
 }
 ]])
 splash:go(args.url)
 return get_div_count()
end

evaljs()与 runjs()
  • evaljs() 以执行JavaScript代码并返回最后一条JavaScript语句的返回结果
  • runjs() 以执行JavaScript代码,它与evaljs()的功能类似,但是更偏向于执行某些动作或声明某些方法
function main(splash, args)
 
 splash:runjs("foo = function() { return 'sxt' }")
 local result = splash:evaljs("foo()")
 return result
end

html()

获取网页的源代码

function main(splash, args)
 splash:go("https://www.bjsxt.com")
 return splash:html()
end

png()

获取PNG格式的网页截图

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

har()

获取页面加载过程描述

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

url()

获取当前正在访问的URL

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

get_cookies()

获取当前页面的Cookies

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

add_cookie()

当前页面添加Cookie

cookies = splash:add_cookie{name, value, path=nil,
 domain=nil, expires=nil, httpOnly=nil, secure=nil}

function main(splash)
  splash:add_cookie{"sessionid", "123456abcdef", "/", domain="http://bjsxt.com"}
  splash:go("http://bjsxt.com/")
  return splash:html()
end

clear_cookies()

可以清除所有的Cookies

function main(splash)
  splash:go("https://www.bjsxt.com/")
  splash:clear_cookies()
  return splash:get_cookies()
end

set_user_agent()

设置浏览器的User-Agent

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

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

select()

选中符合条件的第一个节点

如果有多个节点符合条件,则只会返回一个

其参数是CSS选择器

function main(splash)
 splash:go("https://www.baidu.com/")
 input = splash:select("#kw")
 splash:wait(3)
 return splash:png()
end

send_text()

填写文本

function main(splash)
 splash:go("https://www.baidu.com/")
 input = splash:select("#kw")
 input:send_text('Splash')
 splash:wait(3)
 return splash:png()
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

代理Ip
function main(splash)
  splash:on_request(function(request)
    request:set_proxy{
      host='61.138.33.20',
      port=808,
      username='uanme',
      password='passwrod'
     }
  
   end)
  
  -- 设置请求头
  splash:set_user_agent("Mozilla/5.0")


  splash:go("https://httpbin.org/get")
  return splash:html()
end

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

留不住的人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值