Jsoup模块提供了网络请求更html解析查询等功能,方便在客户端做网络数据爬取,具体可以查询java Jsoup的相关api或教程
Jsoup.connect(url)
- url 网站地址
- 返回值 Connection
Connection
- get() 通过get方式请求 返回 Document
- post() 通过post方式请求 返回 Document
- proxy(String host, int port) // 设置代理
- userAgent(String userAgent); 设置请求user-agent报头。
- timeout(int millis); // 超时时间 默认超时30秒
- data(String key, String value) // 添加请求数据参数。对于get,请求参数在请求查询字符串中发送,对于post,则在请求体中发送。一个请求可以有多个相同名称的值。
- requestBody(String body) //配合 header("Content-Type", "application/json") 发送json请求体
- header(String name, String value); // 设置请求头
- cookie(String name, String value); // 设置cookie
- cookies(Map<String, String> cookies);
- execute() // 发送请求 返回Response
try {
var document = Jsoup.connect('https://www.baidu.com').get()
document.title() // 获取title string
document.head() // 获取head Element
document.body() // 获取body Element
document.select(cssQuery) //通过css选择对应的元素 Elements
document.selectFirst(cssQuery) //通过css选择对应的元素,选第一个 Element
}catch(e){
Console.log(e)
}
Element
- 文档元素对象
text()
- 获取元素包裹的内容如 <span>hello</span> 得到字符串hello
- 返回值 string
attr(string v)
- 获取元素的属性值 如:<div class='hello' >hello world </div> 可以通过 Element.attr('class') 得到字符串 hello
- 返回值 string
parent()
- 返回值 获取父元素 Element
children()
- 返回值 Elements 子元素集
select(cssQuery)
- 通过css规则匹配
- 返回值 Elements
selectFirst
- 通过css规则匹配首个元素
- 返回值 Element