Python全系列 教程
3567个小节阅读:5929.1k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
上图中main()方法的第一个参数是splash,这个对象非常重要,它类似于Selenium中的WebDriver对象
控制页面上下或左右滚动
xxxxxxxxxx
splash.scroll_position = {x=100, y=200}
该方法用来请求某个链接,而且它可以模拟GET和POST请求,同时支持传入请求头、表单等数据
xxxxxxxxxx
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 |
xxxxxxxxxx
splash:go{"http://www.sxt.cn", http_method="POST", body="name=17703181473"}
控制页面的等待时间
xxxxxxxxxx
splash:wait{time, cancel_on_redirect=false, cancel_on_error=true}
参数 | 含义 |
---|---|
time | 等待的秒数 |
cancel_on_redirect | 可选参数,默认为false,表示如果发生了重定向就停止等待,并返回重定向结果 |
cancel_on_error | 可选参数,默认为false,表示如果发生了加载错误,就停止等待 |
xxxxxxxxxx
function main(splash)
splash:go("https://www.taobao.com")
splash:wait(2)
return {html=splash:html()}
end
直接调用JavaScript定义的方法,但是所调用的方法需要用双中括号包围,这相当于实现了JavaScript方法到Lua脚本的转换
xxxxxxxxxx
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
xxxxxxxxxx
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
xxxxxxxxxx
function main(splash, args)
splash:runjs("foo = function() { return 'sxt' }")
local result = splash:evaljs("foo()")
return result
end
获取网页的源代码
xxxxxxxxxx
function main(splash, args)
splash:go("https://www.bjsxt.com")
return splash:html()
end
获取PNG格式的网页截图
xxxxxxxxxx
function main(splash, args)
splash:go("https://www.bjsxt.com")
return splash:png()
end
获取页面加载过程描述
xxxxxxxxxx
function main(splash, args)
splash:go("https://www.bjsxt.com")
return splash:har()
end
获取当前正在访问的URL
xxxxxxxxxx
function main(splash, args)
splash:go("https://www.bjsxt.com")
return splash:url()
end
获取当前页面的Cookies
xxxxxxxxxx
function main(splash, args)
splash:go("https://www.bjsxt.com")
return splash:get_cookies()
end
当前页面添加Cookie
xxxxxxxxxx
cookies = splash:add_cookie{name, value, path=nil, domain=nil, expires=nil, httpOnly=nil, secure=nil}
xxxxxxxxxx
function main(splash)
splash:add_cookie{"sessionid", "123456abcdef", "/", domain="http://bjsxt.com"}
splash:go("http://bjsxt.com/")
return splash:html()
end
可以清除所有的Cookies
xxxxxxxxxx
function main(splash)
splash:go("https://www.bjsxt.com/")
splash:clear_cookies()
return splash:get_cookies()
end
设置浏览器的User-Agent
xxxxxxxxxx
function main(splash)
splash:set_user_agent('Splash')
splash:go("http://httpbin.org/get")
return splash:html()
end
设置请求头
xxxxxxxxxx
function main(splash)
splash:set_custom_headers({
["User-Agent"] = "Splash",
["Site"] = "Splash",
})
splash:go("http://httpbin.org/get")
return splash:html()
end
选中符合条件的第一个节点
如果有多个节点符合条件,则只会返回一个
其参数是CSS选择器
xxxxxxxxxx
function main(splash)
splash:go("https://www.baidu.com/")
input = splash:select("#kw")
splash:wait(3)
return splash:png()
end
填写文本
xxxxxxxxxx
function main(splash)
splash:go("https://www.baidu.com/")
input = splash:select("#kw")
input:send_text('Splash')
splash:wait(3)
return splash:png()
end
模拟鼠标点击操作
xxxxxxxxxx
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
xfunction 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
实时效果反馈
1. 关于Splash基本使用, 说法错误的是?
A go( )方法用于发送请求
B html( )获取代码源码
C select( )选择元素
D proxy( ) 设置代理
答案
1=>D