Basically, Java provides different types of libraries to the user, in which jsoup maven is one of the libraries that are provided by Java. Jsoup normally is used while we need to work with the real-time HTML pages. Jsoup provides the different types of API to fetch the different URLs and manipulates them with the help of HTML5 DOM and a selector of CSS as per requirement. By using jsoup we perform a different operation or we can say that we can write the different programs for getting the title of a web page, get a number of links from a specific web page, get a number of images from the specified web pages and we can get the metadata of URL and HTML documents.
Jsoup Overview
Jsoup is an open-source Java library utilized essentially for separating information from HTML. It additionally permits you to control and yield HTML. It has a consistent improvement line, extraordinary documentation, and a familiar and adaptable API. Jsoup can likewise be utilized to parse and fabricate XML.
Jsoup loads the page HTML and constructs the related DOM tree. This tree works the same way as the DOM in a program, offering techniques like jQuery and vanilla JavaScript to choose the cross, control text/HTML/characteristics and add/eliminate components.
Different components of Jsoup are listed below as follows.
- Stacking: Bringing and parsing the HTML into a Document.
- Separating: Choosing the ideal information into Elements and navigating it.
- Removing: Getting properties, text, and HTML of hubs.
- Changing: Adding/altering/eliminating hubs and altering their traits.
All Jsoup Examples
Now let’s see all the examples jsoup one by one as follows.
Example #1
package demo; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class jsoup { public static void main(String[] args) { String html_code = "<title>Welcome</title>" + "<p>This is jsoup program</p>"; Document docu = Jsoup.parse(html_code); System.out.println(docu.title()); Elements para = docu.getElementsByTag("p"); for (Element paragraph : para) { System.out.println(paragraph.text()); } } }
Explanation
This is a simple program of Jsoup where we try to fetch the content of web pages, in the above example we write the string with HTML code and we try to fetch that string by using Jsoup library as shown in the above code. The end result of the above program we illustrated by using the following screenshot as follows.
Now let’s see the second example of Jsoup as follows.
Example #2
package com.sample; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; public class jsample { public static void main(String[] args) { Document docu; try { // required protocol that is http docu = Jsoup.connect("http://google.com").get(); // title of page String title_page = docu.title(); System.out.println("title : " + title_page); // links Elements links_web = docu.select("a[href]"); for (Element link : links_web) { // href attribute System.out.println("\n web_link : " + link.attr("href")); System.out.println("web_text : " + link.text()); } } catch (IOException e) { e.printStackTrace(); } } }
Explanation
By using the above code we try to find out the all hyperlinks of google.com. Here we first import the required packages and library as shown. After we write the code for HTTP protocol and how we can get the all hyperlinks for google.com as shown. The final output of the above program we illustrated by using the following screenshot as follows.
Now let’s see examples of images as follows.
Example #3
package demo; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; public class jsample { public static void main(String[] args) { Document docu; try { docu = Jsoup.connect("http://google.com").get(); Elements web_images = docu.select("img[src~=(?i)\\.(png|jpe?g|gif)]"); for (Element image : web_images) { System.out.println("\nsrc : " + image.attr("src")); System.out.println("Img_height : " + image.attr("height")); System.out.println("Img_width : " + image.attr("width")); System.out.println("Img_alt : " + image.attr("alt")); } } catch (IOException e) { e.printStackTrace(); } } }
Explanation
By using the above example, we try to fetch all sources of images, here we try to fetch the images of google.com as shown in the above code. After that, we write the code to fetch the height, width as shown. The final output of the above program we illustrated by using the following screenshot as follows.
Now let’s see the example of metadata as follows.
package demo; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; public class jsample { public static void main(String[] args) { StringBuffer html_code = new StringBuffer(); html_code.append(""); html_code.append(""); html_code.append(""); html_code.append("<meta charset='\"UTF-8\"'>"); html_code.append("<title>Hollywood Life</title>"); html_code.append("<meta name='\"description\"' content='\"New' trends in entertainment news>"); html_code.append("<meta name='\"keywords\"' content='\"Cricket' news bollywood football>"); html_code.append(""); html_code.append(""); html_code.append("<div id="color">Blue color is here</div> />"); html_code.append(""); html_code.append(""); Document docu = Jsoup.parse(html_code.toString()); //get a description of metadata content String des = docu.select("meta[name=description]").get(0).attr("content"); System.out.println("Meta description : " + des); //get keyword of metadata content String keyw = docu.select("meta[name=keywords]").first().attr("content"); System.out.println("Meta keyword : " + keyw); String color_A = docu.getElementById("color").text(); String color_B = docu.select("div#color").get(0).text(); System.out.println(color_A); System.out.println(color_B); } }
Explanation
By using the above code we try to implement the metadata in jsoup, here we write the HTML body and metadata and push by using the append function as shown. After that, we write the code for the metadata description and keyword of metadata. The final output of the above program we illustrated by using the following screenshot as follows.
Now let’s see how we can get icons as follows.
Example #4
package demo; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; public class jsample { public static void main(String[] args) { StringBuffer html_code = new StringBuffer(); html_code.append(""); html_code.append(""); html_code.append("<link rel='\"icon\"' href="%5C%22http://specifiedurl.com/img.ico%5C%22">"); html_code.append(""); html_code.append(""); html_code.append("something"); html_code.append(""); html_code.append(""); Document docu = Jsoup.parse(html_code.toString()); String fav = ""; Element ele = docu.head().select("link[href~=.*\\.(ico|png)]").first(); if(ele==null){ ele = docu.head().select("meta[itemprop=image]").first(); if(ele!=null){ fav = ele.attr("content"); } }else{ fav = ele.attr("href"); } System.out.println(fav); } }
Explanation
By using the above example we try to implement the get an icon in Jsoup, here we need to specify the URL of the website that we want. After that, we also need to mention the link tag of HTML as shown. The final output of the above program we illustrated by using the following screenshot as follows.
Conclusion – Jsoup Example
We hope from this article you learn more about the Jsoup example. From the above article, we have taken in the essential idea of the jsoup example. and we also see the representation and example of Jsoup examples. From this article, we learned how and when we use the Jsoup example.
以上是Jsoup 範例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

HTML代碼可以通過在線驗證器、集成工具和自動化流程來確保其清潔度。 1)使用W3CMarkupValidationService在線驗證HTML代碼。 2)在VisualStudioCode中安裝並配置HTMLHint擴展進行實時驗證。 3)利用HTMLTidy在構建流程中自動驗證和清理HTML文件。

HTML、CSS和JavaScript是構建現代網頁的核心技術:1.HTML定義網頁結構,2.CSS負責網頁外觀,3.JavaScript提供網頁動態和交互性,它們共同作用,打造出用戶體驗良好的網站。

HTML的功能是定義網頁的結構和內容,其目的在於提供一種標準化的方式來展示信息。 1)HTML通過標籤和屬性組織網頁的各個部分,如標題和段落。 2)它支持內容與表現分離,提升維護效率。 3)HTML具有可擴展性,允許自定義標籤增強SEO。

HTML的未來趨勢是語義化和Web組件,CSS的未來趨勢是CSS-in-JS和CSSHoudini,JavaScript的未來趨勢是WebAssembly和Serverless。 1.HTML的語義化提高可訪問性和SEO效果,Web組件提升開發效率但需注意瀏覽器兼容性。 2.CSS-in-JS增強樣式管理靈活性但可能增大文件體積,CSSHoudini允許直接操作CSS渲染。 3.WebAssembly優化瀏覽器應用性能但學習曲線陡,Serverless簡化開發但需優化冷啟動問題。

HTML、CSS和JavaScript在Web開發中的作用分別是:1.HTML定義網頁結構,2.CSS控製網頁樣式,3.JavaScript添加動態行為。它們共同構建了現代網站的框架、美觀和交互性。

HTML的未來充滿了無限可能。 1)新功能和標準將包括更多的語義化標籤和WebComponents的普及。 2)網頁設計趨勢將繼續向響應式和無障礙設計發展。 3)性能優化將通過響應式圖片加載和延遲加載技術提升用戶體驗。

HTML、CSS和JavaScript在網頁開發中的角色分別是:HTML負責內容結構,CSS負責樣式,JavaScript負責動態行為。 1.HTML通過標籤定義網頁結構和內容,確保語義化。 2.CSS通過選擇器和屬性控製網頁樣式,使其美觀易讀。 3.JavaScript通過腳本控製網頁行為,實現動態和交互功能。

HTMLISNOTAPROGRAMMENGUAGE; ITISAMARKUMARKUPLAGUAGE.1)htmlStructures andFormatSwebContentusingtags.2)itworkswithcsssforstylingandjavascript for Interactivity,增強WebevebDevelopment。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

Dreamweaver Mac版
視覺化網頁開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。