简体   繁体   中英

Extract data from html code with Jsoup

I want to extract from this HTML code the word Mustafa with Jsoup.

<h1 id="firstHeading" class="firstHeading">Mustafa</h1>
        <!-- /firstHeading -->

How can I do this?

With Jsoup you can use CSS selectors to select elements. An element with id="firstHeading" is selectable with CSS selector #firstHeading .

Thus, this should do:

Document document = Jsoup.parse(html);
String firstHeading = document.select("#firstHeading").text();
System.out.println(firstHeading); // Mustafa

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