【问题标题】:retrieve html inline style attribute value with jsoup使用 jsoup 检索 html 内联样式属性值
【发布时间】:2020-04-25 21:45:40
【问题描述】:

有人帮我用jsoup检索这个例子中text-align样式的值吗?

<th style="text-align:right">4389</th>

这里我要获取值正确

谢谢!

【问题讨论】:

  • 向我们展示到目前为止您已实施的内容。你卡在哪里了。
  • 我想知道我的表达是否与Elements elt = doc.select("[style*='text-align']"); System.out.println(elt.size());

标签: java html jsoup


【解决方案1】:

您可以检索元素的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);

输出:

<th style="text-align:right">4389</th>
text-align:right
right

【讨论】:

  • 这是我最终想到的解决方案,但方式不同。感谢@wartai 的帮助。
  • 这又是对样式值的字符串操作。当附加多个样式时,这将不起作用。在 jsoup 中是否有一种方法可以让我们将单个样式作为 Element 以便 innerStyle.attr("text:align") 应该返回“right”?
  • @SaravanaPrakash 是的,这似乎是未完成的事情。我的意思是,设计一个正则表达式很容易,它将提取多个“正确形成”的键值,但似乎 JSoup 可能希望将其作为标准处理......例如,我现在想知道我必须在哪里寻找找出法律允许哪些字符作为“键”和“值”;假设您可以在冒号的任何一侧有任意空格等:这些细节应该由 JSoup 包处理......
【解决方案2】:

另一种提取样式属性的方法是:

public Map<String, String> getStyleMap(Element element) {
    Map<String, String> keymaps = new HashMap<>();
    if (!element.hasAttr("style")) {
        return keymaps;
    }
    String styleStr = element.attr("style"); // => margin-top:-80px !important;color:#fcc;border-bottom:1px solid #ccc; background-color: #333; text-align:center
    String[] keys = styleStr.split(":");
    String[] split;
    if (keys.length > 1) {
        for (int i = 0; i < keys.length; i++) {
            if (i % 2 != 0) {
                split = keys[i].split(";");
                if (split.length == 1) break;
                keymaps.put(split[1].trim(), keys[i + 1].split(";")[0].trim());
            } else {
                split = keys[i].split(";");
                if (i + 1 == keys.length) break;
                keymaps.put(keys[i].split(";")[split.length - 1].trim(), keys[i + 1].split(";")[0].trim());
            }
        }
    }
    return keymaps;
}

将 HashMap 填充为:

0 = {HashMap$Node@5713} "background-color" -> "#333"
1 = {HashMap$Node@5714} "color" -> "#fcc"
2 = {HashMap$Node@5715} "font-family" -> "'Helvetica Neue', Helvetica, Arial, sans-serif"
3 = {HashMap$Node@5716} "margin-top" -> "-80px !important"
4 = {HashMap$Node@5717} "text-align" -> "center"

【讨论】:

    【解决方案3】:
    public static Map<String, String[]> getStyleMap(String styleStr) {
        Map<String, String[]> keymaps = new HashMap<>();
        // margin-top:-80px !important;color:#fcc;border-bottom:1px solid #ccc; background-color: #333; text-align:center
        String[] list = styleStr.split(":|;");
        for (int i = 0; i < list.length; i+=2) {
            keymaps.put(list[i].trim(),list[i+1].trim().split(" "));
        }
        return keymaps;
    }
    

    结果:

    0 = {HashMap$Node@5713} "background-color" -> ["#333"]
    1 = {HashMap$Node@5714} "color" -> ["#fcc"]
    2 = {HashMap$Node@5716} "margin-top" -> ["-80px","!important"]
    3 = {HashMap$Node@5717} "text-align" -> ["center"]
    

    【讨论】:

      猜你喜欢
      • 2013-11-25
      • 2015-08-10
      • 1970-01-01
      • 2013-11-16
      • 2014-02-18
      • 1970-01-01
      • 1970-01-01
      • 2018-11-30
      • 1970-01-01
      相关资源
      最近更新 更多