android jsoup简书,爬虫之Jsoup

Jsoup简介

jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址、HTML文本内容。它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来取出和操作数据。官网:https://jsoup.org/

主要功能

从一个URL,文件或字符串中解析HTML

使用DOM或CSS选择器来查找、取出数据使用DOM或CSS选择器来查找、取出数据

可操作HTML元素、属性、文本可操作HTML元素、属性、文本

注意:jsoup是基于MIT协议发布的,可放心使用于商业项目。

Maven依赖关系

org.jsoup

jsoup

1.11.3

jsoup api

6个包提供用于开发jsoup应用程序的类和接口。

org.jsoup

org.jsoup.examples

org.jsoup.helper

org.jsoup.nodes

org.jsoup.parser

org.jsoup.safety

org.jsoup.salect

主要类:

Jsoup 类提供了连接,清理和解析HTML文档的方法

Document 获取HTML文档

Element 获取、操作HTML节点

简单学习

三种加载HTML的方法

@Test

public void test1() throws IOException {

//从URL加载HTML

Document document = Jsoup.connect("http://www.baidu.com").get();

String title = document.title();

//获取html中的标题

System.out.println("title :"+title);

//从字符串加载HTML

String html = "

First parse"

+ "

Parsed HTML into a doc.

";

Document doc = Jsoup.parse(html);

title = doc.title();

System.out.println("title :"+title);

//从文件加载HTML

doc = Jsoup.parse(new File("F:\\jsoup\\html\\index.html"),"utf-8");

title = doc.title();

System.out.println("title :"+title);

}

获取html中的head,body,url等信息

@Test

public void test2() throws IOException {

Document document = Jsoup.connect("http://www.baidu.com").get();

String title = document.title();

System.out.println("title :"+title);

//获取html中的head

System.out.println(document.head());

//获取html中的body

//System.out.println(document.body());

//获取HTML页面中的所有链接

Elements links = document.select("a[href]");

for (Element link : links){

System.out.println("link : "+ link.attr("href"));

System.out.println("text :"+ link.text());

}

}

获取URL的元信息

@Test

public void test3() throws IOException {

Document document = Jsoup.connect("https://passport.lagou.com").get();

System.out.println(document.head());

//获取URL的元信息

String description = document.select("meta[name=description]").get(0).attr("content");

System.out.println("Meta description : " + description);

String keywords = document.select("meta[name=keywords]").first().attr("content");

System.out.println("Meta keyword : " + keywords);

}

根据class名称获取表单

@Test

public void test4() throws IOException {

Document document = Jsoup.connect("https://passport.lagou.com/login/login.html?signature=8ECBCDF2B86061432B425A0B94FC863B&service=https%253A%252F%252Fwww.lagou.com%252F&action=login&serviceId=lagou&ts=1547711303033").get();

//获取拉勾网登入页面的body

//System.out.println(document.body());

//根据class名称获取表单

Elements formElement = document.getElementsByClass("form_body");

System.out.println(formElement.html());

//获取URL的元信息

for (Element inputElement : formElement) {

String placeholder = inputElement.getElementsByTag("input").attr("placeholder");

System.out.println(placeholder);

}

}

提取并打印表单参数

@Test

public void test5() throws IOException {

Document document = Jsoup.parse(new File("F:\\jsoup\\html\\login.html"),"utf-8");

Element loginform = document.getElementById("registerform");

Elements inputElements = loginform.getElementsByTag("input");

for (Element inputElement : inputElements) {

String key = inputElement.attr("name");

String value = inputElement.attr("value");

System.out.println("Param name: "+key+" -- Param value: "+value);

}

}

设置元素的html内容

@Test

public void test6() throws IOException {

Document document = Jsoup.parse(new File("F:\\jsoup\\html\\index.html"),"utf-8");

System.out.println(document.body());//

System.out.println("*************");

Element div = document.select("div").first();

div.html("

Hello

"); //

Hello

div.prepend("

Fiest

"); //

Fiest

Hello

div.append("

Last

"); //

Fiest

Hello

Last

System.out.println(document.body());

System.out.println("*************");

System.out.println(div.text());

System.out.println("*************");

//对元素包裹一个外部HTML内容

div.wrap("

Fiest

Hello

Last

System.out.println(document.body());

}

设置元素的文本内容

@Test

public void test7() throws IOException {

Document document = Jsoup.parse(new File("F:\\jsoup\\html\\index.html"),"utf-8");

System.out.println(document.body());//

System.out.println("*************");

Element div = document.select("div").first();

div.text("7 > 8 "); //

7 > 8

div.prepend("Fiest "); //

Fiest 7 > 8

div.append("Last "); //

Fiest 7 > 8 Last

System.out.println(document.body());

System.out.println("*************");

System.out.println(div.text());

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值