Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.open-android:jsoup:jsoup-1.10.2'
}
dependencies {
implementation("com.github.open-android:jsoup:jsoup-1.10.2")
}
<dependency>
<groupId>com.github.open-android</groupId>
<artifactId>jsoup</artifactId>
<version>jsoup-1.10.2</version>
</dependency>
libraryDependencies += "com.github.open-android" % "jsoup" % "jsoup-1.10.2"
:dependencies [[com.github.open-android/jsoup "jsoup-1.10.2"]]
jsoup 是一款 Java 的HTML 解析器,可通过DOM,CSS选择器以及类似于JQuery的操作方法来提取和操作Html文档数据。
开源地址:https://github.com/open-android/Jsoup
爱生活,爱学习,更爱做代码的搬运工,分类查找更方便请下载黑马助手app
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
compile 'com.github.open-android:Jsoup:jsoup-1.10.2'
a.测试用html内容如下
<html>
<head>
<title>First parse</title>
</head>
<body>
<p align="center">attribute parse</p>
<p>text parse</p>
</body>
</html>
b.将演示代码复制到Activity的onCreate方法中
//测试用html字符串
String html = "<html><head><title>First parse</title></head>"
+ "<body><p align=\"center\">attribute parse</p>"
+ "<p>text parse</p></body></html>";
//Jsoup解析获得Document对象
Document doc = Jsoup.parse(html);
System.out.println("解析出来的html:\n"+doc.toString());
//获得head元素对象
Element head = doc.head();
//DOM方式获得第一个title元素
Element title = head.getElementsByTag("title").first();
//获得title元素中文本
String text = title.text();
System.out.println("title标签中文本: " + text);
//---------------------------------------
//获得body元素对象
Element body = doc.body();
//选择器语法查找p元素
Elements lists = body.select("p");
//遍历所有p元素,输出p元素文本
for(Element p : lists){
System.out.println("p元素文本: " + p.text());
}
//选择器语法查找第一个拥有align属性的p元素
Element pElement = body.select("p[align]").first();
//获得p元素align属性值
String align = pElement.attr("align");
System.out.println("p元素align属性值: " + align);
注意:如果解析指定url需要添加网络访问权限