如果样式嵌入在Element中,则只需使用.attr(“style”).
JSoup不是Html渲染器,它只是一个HTML解析器,因此您必须从检索到的< style>中解析内容.标记html内容.你可以使用一个简单的正则表达式;但它并不适用于所有情况.您可能希望使用CSS解析器执行此任务.
public class Test {
public static void main(String[] args) throws Exception {
String html = "\n" +
"
\n"+"
Page 1\n"+"\n"+
"
\n"+
"
"\n"+
"\n"+
"\n"+
"";
Document doc = Jsoup.parse(html);
Element style = doc.select("style").first();
Matcher cssMatcher = Pattern.compile("[.](\\w+)\\s*[{]([^}]+)[}]").matcher(style.html());
while (cssMatcher.find()) {
System.out.println("Style `" + cssMatcher.group(1) + "`: " + cssMatcher.group(2));
}
}
}
将输出:
Style `ft00`: font-size:11px;font-family:Times;color:#ffffff;
Style `ft01`: font-size:11px;font-family:Times;color:#ffffff;