使用jsoup检索html内联样式属性值

Pap*_*sse 5 html java jsoup

有人帮助我使用jsoup检索此示例中text-align样式的值吗?

<th style="text-align:right">4389</th>
Run Code Online (Sandbox Code Playgroud)

在这里,我希望得到正确的价值

谢谢!

小智 6

您可以检索style元素的属性,然后将其拆分:.

例:

final String html = "<th style=\"text-align:right\">4389</th>";

Document doc = Jsoup.parse(html, "", Parser.xmlParser()); // Using the default html parser may remove the style attribute
Element th = doc.select("th[style]").first();


String style = th.attr("style"); // You can put those two lines into one
String styleValue = style.split(":")[1]; // TODO: Insert a check if a value is set

// Output the results
System.out.println(th);
System.out.println(style);
System.out.println(styleValue);
Run Code Online (Sandbox Code Playgroud)

输出:

<th style="text-align:right">4389</th>
text-align:right
right
Run Code Online (Sandbox Code Playgroud)