1. Jsoup import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class HtmlParserExample { public static void main(String[] args) { String html = "<html><head><title>Jsoup Example</title></head><body><p>Example HTML</p></body></html>"; Document document = Jsoup.parse(html); Element bodyElement = document.body(); Element pElement = bodyElement.selectFirst("p"); String text = pElement.text(); System.out.println(text); } } 2. HtmlUnit import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlPage; public class HtmlParserExample { public static void main(String[] args) { try (WebClient webClient = new WebClient()) { HtmlPage page = webClient.getPage("https://example.com"); String pageContent = page.asXml(); System.out.println(pageContent); } catch (Exception e) { e.printStackTrace(); } } }


上一篇:
下一篇:
切换中文