如何使用Jsoup从HTML内容中获取CSS样式属性。

huangapple go评论82阅读模式
英文:

How to get the CSS style attribute from HTML content using Jsoup

问题

String style = Element.attr("style");
String[] styleProperties = style.split(";");
for (String property : styleProperties) {
    if (property.trim().startsWith("background:")) {
        String backgroundValue = property.trim().replace("background:", "");
        // Use backgroundValue as needed
        break;
    }
}
英文:

I'm trying to get the Style attribute from the HTML div tag using Jsoup. See the below is my div tag.

<div id="badge" class="er-badge" style="width: 576px; height: 369px; background: url("http://localhost:1020/er/ERImg/firmUploads/13854/LogoImg4.png") 0% 0% / cover no-repeat; z-index: 1; position: relative;" tabindex="0" data-preview-purposes="checked">

from this div tag style I want to extract only the background: attribute. I can fetch the entire style properties using below code

String style = Element.attr("style");

But, I want to fetch only the background attribute value from this. Is it possible to extract that using Jsoup or Please tell any other easy way to extract the attribute.
Thanks in advance.

答案1

得分: 0

以下是您要翻译的内容:

高兴看到这个问题,而不是有人试图使用正则表达式手动解析。

就像我们不应该手动解析HTML一样,手动解析CSS也是不可取的。

已经有可用的库 - <del>cssparser</del> htmlunit-cssparser 来完成这项工作。我们可以获取background的CSS值如下:

import org.htmlunit.cssparser.dom.CSSStyleDeclarationImpl;
import org.htmlunit.cssparser.parser.CSSOMParser;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;

import java.io.IOException;

public class ParseStyle {
    public static void main(String[] args) throws IOException {
        Element e = Jsoup.parseBodyFragment(&quot;&quot;&quot;
                &lt;div id=&quot;badge&quot; class=&quot;er-badge&quot; style=&quot;width: 576px; height: 369px; background: url(&amp;quot;http://localhost:1020/er/ERImg/firmUploads/13854/LogoImg4.png&amp;quot;) 0% 0% / cover no-repeat; z-index: 1; position: relative;&quot; tabindex=&quot;0&quot; data-preview-purposes=&quot;checked&quot;&gt; 
                 &quot;&quot;&quot;);
        String style = e.select(&quot;div&quot;).attr(&quot;style&quot;);
        CSSOMParser parser = new CSSOMParser();
        CSSStyleDeclarationImpl decl = parser.parseStyleDeclaration(style);
        System.out.println(decl.getPropertyCSSValue(&quot;background&quot;));
    }
}
英文:

Happy to see this question instead of someone trying to use Regex to parse manually.

Just like we should not parse html ourself, parsing CSS manually is also undesired.

There is already available library - <del>cssparser</del> htmlunit-cssparser to do the job. We can get the CSS value of background as below:

import org.htmlunit.cssparser.dom.CSSStyleDeclarationImpl;
import org.htmlunit.cssparser.parser.CSSOMParser;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;

import java.io.IOException;

public class ParseStyle {
    public static void main(String[] args) throws IOException {
        Element e = Jsoup.parseBodyFragment(&quot;&quot;&quot;
                &lt;div id=&quot;badge&quot; class=&quot;er-badge&quot; style=&quot;width: 576px; height: 369px; background: url(&amp;quot;http://localhost:1020/er/ERImg/firmUploads/13854/LogoImg4.png&amp;quot;) 0% 0% / cover no-repeat; z-index: 1; position: relative;&quot; tabindex=&quot;0&quot; data-preview-purposes=&quot;checked&quot;&gt; 
                 &quot;&quot;&quot;);
        String style = e.select(&quot;div&quot;).attr(&quot;style&quot;);
        CSSOMParser parser = new CSSOMParser();
        CSSStyleDeclarationImpl decl = parser.parseStyleDeclaration(style);
        System.out.println(decl.getPropertyCSSValue(&quot;background&quot;));
    }
}

huangapple
  • 本文由 发表于 2023年4月13日 22:11:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定