jsoup 简介
Java 程序在解析 HTML 文档时,相信你们都接触过 htmlparser 这个开源项目,我曾经在 IBM DW 上发表过两篇关于 htmlparser 的文章,分别是:从 HTML 中攫取你所需的信息和 扩展 HTMLParser 对自定义标签的处理能力。但如今我已经再也不使用 htmlparser 了,缘由是 htmlparser 不多更新,但最重要的是有了 jsoup 。
jsoup 是一款 Java 的 HTML 解析器,可直接解析某个 URL 地址、HTML 文本内容。它提供了一套很是省力的 API,可经过 DOM,CSS 以及相似于 jQuery 的操做方法来取出和操做数据。
jsoup 的主要功能以下:
1. 从一个 URL,文件或字符串中解析 HTML;
2. 使用 DOM 或 CSS 选择器来查找、取出数据;
3. 可操做 HTML 元素、属性、文本;php
官网地址:http://www.open-open.com/jsoup/ css
jsoup Cookbook(中文版)
入门
1.解析和遍历一个html文档
输入
2.解析一个html字符串
3.解析一个body片段
4.根据一个url加载Document对象
5.根据一个文件加载Document对象
数据抽取
6.使用dom方法来遍历一个Document对象
7.使用选择器语法来查找元素
8.从元素集合抽取属性、文本和html内容
9.URL处理
10.程序示例:获取全部连接
数据修改
11.设置属性值
12.设置元素的html内容
13.设置元素的文本内容
html清理
14.消除不受信任的html (来防止xss攻击)html
1.解析和遍历一个HTML文档
如何解析一个HTML文档:java
1
2
3
|
String html =
"<html><head><title>First parse</title></head>"
+
"<body><p>Parsed HTML into a doc.</p></body></html>"
;
Document doc = Jsoup.parse(html);
|
(更详细内容可查看 解析一个HTML字符串.)node
其解析器可以尽最大可能从你提供的HTML文档来创见一个干净的解析结果,不管HTML的格式是否完整。好比它能够处理:jquery
没有关闭的标签 (好比: <p>Lorem <p>Ipsum parses to <p>Lorem</p> <p>Ipsum</p>)
隐式标签 (好比. 它能够自动将 <td>Table data</td>包装成<table><tr><td>?)
建立可靠的文档结构(html标签包含head 和 body,在head只出现恰当的元素)
一个文档的对象模型
文档由多个Elements和TextNodes组成 (以及其它辅助nodes:详细可查看:nodes package tree).
其继承结构以下:Document继承Element继承Node. TextNode继承 Node.
一个Element包含一个子节点集合,并拥有一个父Element。他们还提供了一个惟一的子元素过滤列表。
参见
数据抽取:DOM遍历
数据抽取:Selector syntaxcss3
2.解析一个HTML字符串
存在问题
来自用户输入,一个文件或一个网站的HTML字符串,你可能须要对它进行解析并取其内容,或校验其格式是否完整,或想修改它。怎么办?jsonu可以帮你轻松解决这些问题web
解决方法
使用静态Jsoup.parse(String html) 方法或 Jsoup.parse(String html, String baseUri)示例代码:正则表达式
1
2
3
|
String html =
"<html><head><title>First parse</title></head>"
+
"<body><p>Parsed HTML into a doc.</p></body></html>"
;
Document doc = Jsoup.parse(html);
|
描述django
parse(String html, String baseUri) 这方法可以将输入的HTML解析为一个新的文档 (Document),参数 baseUri 是用来将相对 URL 转成绝对URL,并指定从哪一个网站获取文档。如这个方法不适用,你可使用 parse(String html) 方法来解析成HTML字符串如上面的示例。.
只要解析的不是空字符串,就能返回一个结构合理的文档,其中包含(至少) 一个head和一个body元素。
一旦拥有了一个Document,你就可使用Document中适当的方法或它父类 Element和Node中的方法来取得相关数据。
3.解析一个body片段
问题
假如你有一个HTML片段 (好比. 一个 div 包含一对 p 标签; 一个不完整的HTML文档) 想对它进行解析。这个HTML片段能够是用户提交的一条评论或在一个CMS页面中编辑body部分。
办法
使用Jsoup.parseBodyFragment(String html)方法.
1
2
3
|
String html =
"<div><p>Lorem ipsum.</p>"
;
Document doc = Jsoup.parseBodyFragment(html);
Element body = doc.body();
|
说明
parseBodyFragment 方法建立一个空壳的文档,并插入解析过的HTML到body元素中。假如你使用正常的Jsoup.parse(String html) 方法,一般你也能够获得相同的结果,可是明确将用户输入做为 body片断处理,以确保用户所提供的任何糟糕的HTML都将被解析成body元素。
Document.body() 方法可以取得文档body元素的全部子元素,与 doc.getElementsByTag("body")相同。
保证安全Stay safe
假如你可让用户输入HTML内容,那么要当心避免跨站脚本攻击。利用基于 Whitelist 的清除器和 clean(String bodyHtml, Whitelist whitelist)方法来清除用户输入的恶意内容。
4.从一个URL加载一个Document
你须要从一个网站获取和解析一个HTML文档,并查找其中的相关数据。你可使用下面解决方法:
使用 Jsoup.connect(String url)
方法:
1
2
|
Document doc = Jsoup.connect(
"http://example.com/"
).get();
String title = doc.title();
|
说明
connect(String url)
方法建立一个新的 Connection
, 和 get()
取得和解析一个HTML文件。若是从该URL获取HTML时发生错误,便会抛出 IOException,应适当处理。
Connection
接口还提供一个方法链来解决特殊请求,具体以下:
1
2
3
4
5
6
|
Document doc = Jsoup.connect(
"http://example.com"
)
.data(
"query"
,
"Java"
)
.userAgent(
"Mozilla"
)
.cookie(
"auth"
,
"token"
)
.timeout(
3000
)
.post();
|
这个方法只支持Web URLs (http
和https
协议); 假如你须要从一个文件加载,可使用 parse(File in, String charsetName)
代替。
5.从一个文件加载一个文档
在本机硬盘上有一个HTML文件,须要对它进行解析从中抽取数据或进行修改。
可使用静态 Jsoup.parse(File in, String charsetName, String baseUri)
方法:
1
2
|
File input =
new
File(
"/tmp/input.html"
);
Document doc = Jsoup.parse(input,
"UTF-8"
,
"http://example.com/"
);
|
说明
parse(File in, String charsetName, String baseUri)
这个方法用来加载和解析一个HTML文件。如在加载文件的时候发生错误,将抛出IOException,应做适当处理。
baseUri
参数用于解决文件中URLs是相对路径的问题。若是不须要能够传入一个空的字符串。
另外还有一个方法parse(File in, String charsetName)
,它使用文件的路径作为 baseUri
。 这个方法适用于若是被解析文件位于网站的本地文件系统,且相关连接也指向该文件系统。
6.使用DOM方法来遍历一个文档
你有一个HTML文档要从中提取数据,并了解这个HTML文档的结构。
将HTML解析成一个Document
以后,就可使用相似于DOM的方法进行操做。示例代码:
1
2
3
4
5
6
7
8
9
|
File input =
new
File(
"/tmp/input.html"
);
Document doc = Jsoup.parse(input,
"UTF-8"
,
"http://example.com/"
);
Element content = doc.getElementById(
"content"
);
Elements links = content.getElementsByTag(
"a"
);
for
(Element link : links) {
String linkHref = link.attr(
"href"
);
String linkText = link.text();
}
|
说明
Elements这个对象提供了一系列相似于DOM的方法来查找元素,抽取并处理其中的数据。具体以下:
getElementById(String id)
getElementsByTag(String tag)
getElementsByClass(String className)
getElementsByAttribute(String key)
(and related methods)siblingElements()
, firstElementSibling()
, lastElementSibling()
;nextElementSibling()
, previousElementSibling()
parent()
, children()
, child(int index)
attr(String key)
获取属性attr(String key, String value)
设置属性attributes()
获取全部属性id()
, className()
and classNames()
text()
获取文本内容text(String value)
设置文本内容html()
获取元素内HTMLhtml(String value)
设置元素内的HTML内容outerHtml()
获取元素外HTML内容data()
获取数据内容(例如:script和style标签)tag()
and tagName()
append(String html)
, prepend(String html)
appendText(String text)
, prependText(String text)
appendElement(String tagName)
, prependElement(String tagName)
html(String value)
你想使用相似于CSS或jQuery的语法来查找和操做元素。
可使用Element.select(String selector)
和 Elements.select(String selector)
方法实现:
1
2
3
4
5
6
7
8
9
10
11
|
File input =
new
File(
"/tmp/input.html"
);
Document doc = Jsoup.parse(input,
"UTF-8"
,
"http://example.com/"
);
Elements links = doc.select(
"a[href]"
);
//带有href属性的a元素
Elements pngs = doc.select(
"img[src$=.png]"
);
//扩展名为.png的图片
Element masthead = doc.select(
"div.masthead"
).first();
//class等于masthead的div标签
Elements resultLinks = doc.select(
"h3.r > a"
);
//在h3元素以后的a元素
|
说明
jsoup elements对象支持相似于CSS (或jquery)的选择器语法,来实现很是强大和灵活的查找功能。.
这个select
方法在Document
, Element
,或Elements
对象中均可以使用。且是上下文相关的,所以可实现指定元素的过滤,或者链式选择访问。
Select方法将返回一个Elements
集合,并提供一组方法来抽取和处理结果。
tagname
: 经过标签查找元素,好比:a
ns|tag
: 经过标签在命名空间查找元素,好比:能够用 fb|name
语法来查找 <fb:name>
元素#id
: 经过ID查找元素,好比:#logo
.class
: 经过class名称查找元素,好比:.masthead
[attribute]
: 利用属性查找元素,好比:[href]
[^attr]
: 利用属性名前缀来查找元素,好比:能够用[^data-]
来查找带有HTML5 Dataset属性的元素[attr=value]
: 利用属性值来查找元素,好比:[width=500]
[attr^=value]
, [attr$=value]
, [attr*=value]
: 利用匹配属性值开头、结尾或包含属性值来查找元素,好比:[href*=/path/]
[attr~=regex]
: 利用属性值匹配正则表达式来查找元素,好比: img[src~=(?i)\.(png|jpe?g)]
*
: 这个符号将匹配全部元素el#id
: 元素+ID,好比: div#logo
el.class
: 元素+class,好比: div.masthead
el[attr]
: 元素+class,好比: a[href]
a[href].highlight
ancestor child
: 查找某个元素下子元素,好比:能够用.body p
查找在"body"元素下的全部 p
元素parent > child
: 查找某个父元素下的直接子元素,好比:能够用div.content > p
查找 p
元素,也能够用body > *
查找body标签下全部直接子元素siblingA + siblingB
: 查找在A元素以前第一个同级元素B,好比:div.head + div
siblingA ~ siblingX
: 查找A元素以前的同级X元素,好比:h1 ~ p
el, el, el
:多个选择器组合,查找匹配任一选择器的惟一元素,例如:div.masthead, div.logo
:lt(n)
: 查找哪些元素的同级索引值(它的位置在DOM树中是相对于它的父节点)小于n,好比:td:lt(3)
表示小于三列的元素:gt(n)
:查找哪些元素的同级索引值大于n
,好比
: div p:gt(2)
表示哪些div中有包含2个以上的p元素:eq(n)
: 查找哪些元素的同级索引值与n
相等,好比:form input:eq(1)
表示包含一个input标签的Form元素:has(seletor)
: 查找匹配选择器包含元素的元素,好比:div:has(p)
表示哪些div包含了p元素:not(selector)
: 查找与选择器不匹配的元素,好比: div:not(.logo)
表示不包含 class="logo" 元素的全部 div 列表:contains(text)
: 查找包含给定文本的元素,搜索不区分大不写,好比: p:contains(jsoup)
:containsOwn(text)
: 查找直接包含给定文本的元素:matches(regex)
: 查找哪些元素的文本匹配指定的正则表达式,好比:div:matches((?i)login)
:matchesOwn(regex)
: 查找自身包含文本匹配指定正则表达式的元素能够查看Selector
API参考来了解更详细的内容
在解析得到一个Document实例对象,并查找到一些元素以后,你但愿取得在这些元素中的数据。
Node.attr(String key)
方法Element.text()
方法Element.html()
, 或 Node.outerHtml()
方法示例:
1
2
3
4
5
6
7
8
9
10
11
|
String html =
"<p>An <a href='http://example.com/'><b>example</b></a> link.</p>"
;
Document doc = Jsoup.parse(html);
//解析HTML字符串返回一个Document实现
Element link = doc.select(
"a"
).first();
//查找第一个a元素
String text = doc.body().text();
// "An example link"//取得字符串中的文本
String linkHref = link.attr(
"href"
);
// "http://example.com/"//取得连接地址
String linkText = link.text();
// "example""//取得连接地址中的文本
String linkOuterH = link.outerHtml();
// "<a href="http://example.com"><b>example</b></a>"
String linkInnerH = link.html();
// "<b>example</b>"//取得连接内的html内容
|
说明
上述方法是元素数据访问的核心办法。此外还其它一些方法可使用:
这些访问器方法都有相应的setter方法来更改数据.
Element
和Elements
集合类的参考文档你有一个包含相对URLs路径的HTML文档,须要将这些相对路径转换成绝对路径的URLs。
base URI
,而后abs:
属性前缀来取得包含base URI
的绝对路径。代码以下:
1
2
3
4
5
|
Document doc = Jsoup.connect(
"http://www.open-open.com"
).get();
Element link = doc.select(
"a"
).first();
String relHref = link.attr(
"href"
);
// == "/"
String absHref = link.attr(
"abs:href"
);
// "http://www.open-open.com/"
|
说明
在HTML元素中,URLs常常写成相对于文档位置的相对路径: <a href="/download">...</a>
. 当你使用 Node.attr(String key)
方法来取得a元素的href属性时,它将直接返回在HTML源码中指定定的值。
假如你须要取得一个绝对路径,须要在属性名前加 abs:
前缀。这样就能够返回包含根路径的URL地址attr("abs:href")
所以,在解析HTML文档时,定义base URI很是重要。
若是你不想使用abs:
前缀,还有一个方法可以实现一样的功能 Node.absUrl(String key)
。
这个示例程序将展现如何从一个URL得到一个页面。而后提取页面中的全部连接、图片和其它辅助内容。并检查URLs和文本信息。
运行下面程序须要指定一个URLs做为参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
import
org.jsoup.Jsoup;
import
org.jsoup.helper.Validate;
import
org.jsoup.nodes.Document;
import
org.jsoup.nodes.Element;
import
org.jsoup.select.Elements;
import
java.io.IOException;
/**
* Example program to list links from a URL.
*/
public
class
ListLinks {
public
static
void
main(String[] args)
throws
IOException {
// Validate.isTrue(args.length == 1, "usage: supply url to fetch");
String url =
"http://news.ycombinator.com/"
;
print(
"Fetching %s..."
, url);
Document doc = Jsoup.connect(url).get();
Elements links = doc.select(
"a[href]"
);
//"a[href]" //带有href属性的a元素
Elements media = doc.select(
"[src]"
);
//利用属性查找元素,好比:[href]
Elements imports = doc.select(
"link[href]"
);
print(
"\nMedia: (%d)"
, media.size());
for
(Element src : media) {
if
(src.tagName().equals(
"img"
))
print(
" * %s: <%s> %sx%s (%s)"
,
src.tagName(), src.attr(
"abs:src"
), src.attr(
"width"
), src.attr(
"height"
),
trim(src.attr(
"alt"
),
20
));
//src.attr("src")结果:<y18.gif> 18x18 ()
//src.attr("abs:src")结果:<https://news.ycombinator.com/y18.gif> 18x18 ()
else
print(
" * %s: <%s>"
, src.tagName(), src.attr(
"abs:src"
));
}
print(
"\nImports: (%d)"
, imports.size());
for
(Element link : imports) {
print(
" * %s <%s> (%s)"
, link.tagName(),link.attr(
"abs:href"
), link.attr(
"rel"
));
}
print(
"\nLinks: (%d)"
, links.size());
for
(Element link : links) {
print(
" * a: <%s> (%s)"
, link.attr(
"abs:href"
), trim(link.text(),
35
));
}
}
private
static
void
print(String msg, Object... args) {
System.out.println(String.format(msg, args));
}
private
static
String trim(String s,
int
width) {
if
(s.length() > width)
return
s.substring(
0
, width-
1
) +
"."
;
else
return
s;
}
}
//org/jsoup/examples/ListLinks.java
|
示例输入结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
Fetching http:
//news.ycombinator.com/...
Media: (
38
)
* img: <http:
//ycombinator.com/images/y18.gif> 18x18 ()
* img: <http:
//ycombinator.com/images/s.gif> 10x1 ()
* img: <http:
//ycombinator.com/images/grayarrow.gif> x ()
* img: <http:
//ycombinator.com/images/s.gif> 0x10 ()
* script: <http:
//www.co2stats.com/propres.php?s=1138>
* img: <http:
//ycombinator.com/images/s.gif> 15x1 ()
* img: <http:
//ycombinator.com/images/hnsearch.png> x ()
* img: <http:
//ycombinator.com/images/s.gif> 25x1 ()
* img: <http:
//mixpanel.com/site_media/images/mixpanel_partner_logo_borderless.gif> x (Analytics by Mixpan.)
Imports: (
2
)
* link <http:
//ycombinator.com/news.css> (stylesheet)
* link <http:
//ycombinator.com/favicon.ico> (shortcut icon)
Links: (
141
)
* a: <http:
//ycombinator.com> ()
* a: <http:
//news.ycombinator.com/news> (Hacker News)
* a: <http:
//news.ycombinator.com/newest> (new)
* a: <http:
//news.ycombinator.com/newcomments> (comments)
* a: <http:
//news.ycombinator.com/leaders> (leaders)
* a: <http:
//news.ycombinator.com/jobs> (jobs)
* a: <http:
//news.ycombinator.com/submit> (submit)
* a: <http:
//news.ycombinator.com/x?fnid=JKhQjfU7gW> (login)
* a: <http:
//news.ycombinator.com/vote?for=1094578&dir=up&whence=%6e%65%77%73> ()
* a: <http:
//www.readwriteweb.com/archives/facebook_gets_faster_debuts_homegrown_php_compiler.php?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+readwriteweb+%28ReadWriteWeb%29&utm_content=Twitter> (Facebook speeds up PHP)
* a: <http:
//news.ycombinator.com/user?id=mcxx> (mcxx)
* a: <http:
//news.ycombinator.com/item?id=1094578> (9 comments)
* a: <http:
//news.ycombinator.com/vote?for=1094649&dir=up&whence=%6e%65%77%73> ()
* a: <http:
//groups.google.com/group/django-developers/msg/a65fbbc8effcd914> ("Tough. Django produces XHTML.")
* a: <http:
//news.ycombinator.com/user?id=andybak> (andybak)
* a: <http:
//news.ycombinator.com/item?id=1094649> (3 comments)
* a: <http:
//news.ycombinator.com/vote?for=1093927&dir=up&whence=%6e%65%77%73> ()
* a: <http:
//news.ycombinator.com/x?fnid=p2sdPLE7Ce> (More)
* a: <http:
//news.ycombinator.com/lists> (Lists)
* a: <http:
//news.ycombinator.com/rss> (RSS)
* a: <http:
//ycombinator.com/bookmarklet.html> (Bookmarklet)
* a: <http:
//ycombinator.com/newsguidelines.html> (Guidelines)
* a: <http:
//ycombinator.com/newsfaq.html> (FAQ)
* a: <http:
//ycombinator.com/newsnews.html> (News News)
* a: <http:
//news.ycombinator.com/item?id=363> (Feature Requests)
* a: <http:
//ycombinator.com> (Y Combinator)
* a: <http:
//ycombinator.com/w2010.html> (Apply)
* a: <http:
//ycombinator.com/lib.html> (Library)
* a: <http:
//www.webmynd.com/html/hackernews.html> ()
* a: <http:
//mixpanel.com/?from=yc> ()
|
在你解析一个Document以后可能想修改其中的某些属性值,而后再保存到磁盘或都输出到前台页面。
可使用属性设置方法 Element.attr(String key, String value)
, 和 Elements.attr(String key, String value)
.
假如你须要修改一个元素的 class
属性,可使用 Element.addClass(String className)
和Element.removeClass(String className)
方法。
Elements
提供了批量操做元素属性和class的方法,好比:要为div中的每个a元素都添加一个rel="nofollow"
可使用以下方法:
1
|
doc.select(
"div.comments a"
).attr(
"rel"
,
"nofollow"
);
|
说明
与Element
中的其它方法同样,attr
方法也是返回当 Element
(或在使用选择器是返回 Elements
集合)。这样可以很方便使用方法连用的书写方式。好比:
1
|
doc.select(
"div.masthead"
).attr(
"title"
,
"jsoup"
).addClass(
"round-box"
);
|
你须要一个元素中的HTML内容
可使用Element
中的HTML设置方法具体以下:
1
2
3
4
5
6
7
8
9
|
Element div = doc.select(
"div"
).first();
// <div></div>
div.html(
"<p>lorem ipsum</p>"
);
// <div><p>lorem ipsum</p></div>
div.prepend(
"<p>First</p>"
);
//在div前添加html内容
div.append(
"<p>Last</p>"
);
//在div以后添加html内容
// 添完后的结果: <div><p>First</p><p>lorem ipsum</p><p>Last</p></div>
Element span = doc.select(
"span"
).first();
// <span>One</span>
span.wrap(
"<li><a href='http://example.com/'></a></li>"
);
// 添完后的结果: <li><a href="http://example.com"><span>One</span></a></li>
|
说明
Element.html(String html)
这个方法将先清除元素中的HTML内容,而后用传入的HTML代替。Element.prepend(String first)
和 Element.append(String last)
方法用于在分别在元素内部HTML的前面和后面添加HTML内容Element.wrap(String around)
对元素包裹一个外部HTML内容。能够查看API参考文档中 Element.prependElement(String tag)
和Element.appendElement(String tag)
方法来建立新的元素并做为文档的子元素插入其中。
你须要修改一个HTML文档中的文本内容
可使用Element
的设置方法::
1
2
3
4
5
|
Element div = doc.select(
"div"
).first();
// <div></div>
div.text(
"five > four"
);
// <div>five > four</div>
div.prepend(
"First "
);
div.append(
" Last"
);
// now: <div>First five > four Last</div>
|
说明
文本设置方法与 HTML setter 方法同样:
Element.text(String text)
将清除一个元素中的内部HTML内容,而后提供的文本进行代替Element.prepend(String first)
和 Element.append(String last)
将分别在元素的内部html先后添加文本节点。对于传入的文本若是含有像 <
, >
等这样的字符,将以文本处理,而非HTML。
在作网站的时候,常常会提供用户评论的功能。有些不怀好意的用户,会搞一些脚本到评论内容中,而这些脚本可能会破坏整个页面的行为,更严重的是获取一些机要信息,此时须要清理该HTML,以免跨站脚本cross-site scripting攻击(XSS)。
使用jsoup HTML Cleaner
方法进行清除,但须要指定一个可配置的 Whitelist
。
1
2
3
4
|
String unsafe =
"<p><a href='http://example.com/' onclick='stealCookies()'>Link</a></p>"
;
String safe = Jsoup.clean(unsafe, Whitelist.basic());
// now: <p><a href="http://example.com/" rel="nofollow">Link</a></p>
|
说明
XSS又叫CSS (Cross Site Script) ,跨站脚本攻击。它指的是恶意攻击者往Web页面里插入恶意html代码,当用户浏览该页之时,嵌入其中Web里面的html代码会被执行,从而达到恶意攻击用户的特殊目的。XSS属于被动式的攻击,由于其被动且很差利用,因此许多人常忽略其危害性。因此咱们常常只让用户输入纯文本的内容,但这样用户体验就比较差了。
一个更好的解决方法就是使用一个富文本编辑器WYSIWYG如CKEditor 和 TinyMCE。这些能够输出HTML并可以让用户可视化编辑。虽然他们能够在客户端进行校验,可是这样还不够安全,须要在服务器端进行校验并清除有害的HTML代码,这样才能确保输入到你网站的HTML是安全的。不然,攻击者可以绕过客户端的Javascript验证,并注入不安全的HMTL直接进入您的网站。
jsoup的whitelist清理器可以在服务器端对用户输入的HTML进行过滤,只输出一些安全的标签和属性。
jsoup提供了一系列的Whitelist
基本配置,可以知足大多数要求;但若有必要,也能够进行修改,不过要当心。
这个cleaner很是好用不只能够避免XSS攻击,还能够限制用户能够输入的标签范围。
Cleaner
,了解如何返回一个 Document
对象,而不是字符串Whitelist
,了解如何建立一个自定义的whitelist
转发请注明出处:http://www.cnblogs.com/jycboy/p/jsoupdoc.html