简体   繁体   中英

How to receive data attribute with jsoup?

i need to get data attribute from html I am trying to get like this

Elements element = document.select("div.highlight padding standard-box");
result+= element.attr("data-highlight-embed");

But result is empty, should be data-highlight-embed = content

html-code

<div class="highlight padding standard-box" data-link-tracking-page="Matchpage" 
data-link-tracking-column="[Main content]" data-link-tracking-destination="Click on highlight [button]" 
data-highlight-embed="content">text</div>

You need to change your CSS query and notice that the select() method return multiple elements.

Update the CSS query to

Elements element = document.select("div.highlight.padding.standard-box");

Then you can loop the result

for(Element el : element) {
    System.out.println(el.attr("data-highlight-embed"));
}

Or you can get the first element

System.out.println(element.first().attr("data-highlight-embed"));

To get the data attributes you also can reference how to use dataset() method at https://simplesolution.dev/java-jsoup-extract-custom-data-attributes-html5-element/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM