英文:
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(&quot;http://localhost:1020/er/ERImg/firmUploads/13854/LogoImg4.png&quot;) 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("""
<div id="badge" class="er-badge" style="width: 576px; height: 369px; background: url(&quot;http://localhost:1020/er/ERImg/firmUploads/13854/LogoImg4.png&quot;) 0% 0% / cover no-repeat; z-index: 1; position: relative;" tabindex="0" data-preview-purposes="checked">
""");
String style = e.select("div").attr("style");
CSSOMParser parser = new CSSOMParser();
CSSStyleDeclarationImpl decl = parser.parseStyleDeclaration(style);
System.out.println(decl.getPropertyCSSValue("background"));
}
}
英文:
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("""
<div id="badge" class="er-badge" style="width: 576px; height: 369px; background: url(&quot;http://localhost:1020/er/ERImg/firmUploads/13854/LogoImg4.png&quot;) 0% 0% / cover no-repeat; z-index: 1; position: relative;" tabindex="0" data-preview-purposes="checked">
""");
String style = e.select("div").attr("style");
CSSOMParser parser = new CSSOMParser();
CSSStyleDeclarationImpl decl = parser.parseStyleDeclaration(style);
System.out.println(decl.getPropertyCSSValue("background"));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论