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
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)