我正在使用 java 代码从网络中提取信息进行处理,并且我正在使用 jsoup
库来清理从网站获得的响应中的 html 标签。现在,为了从这些代码中提取信息,我必须将 html 标签替换为很少使用的字符,例如“~”。
这是我的问题:
如何转换:
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<h3>This is heading 3</h3>
<h4>This is heading 4</h4>
<h5>This is heading 5</h5>
<h6>This is heading 6</h6>
进入此:
~This is heading 1~
~This is heading 2~
~This is heading 3~
~This is heading 4~
~This is heading 5~
~This is heading 6~
使用jsoup
?
最佳答案
String cssSelector = //add your selector. from the example you include i cant get a proper selector.
Document doc = Jsoup.parse("html")
Elements elms = doc.select(cssSelector)
for(Element elm:elms){
System.out.println("~" + elm.text() + "~")
}
更新
如果你想替换所有元素,你可以这样做:
html = html.replaceAll("<[^>]*>","~")
关于java - 如何在java中使用jsoup用字符替换html标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19176365/