Home > Java > javaTutorial > How to use JSoup function for HTML parsing in Java

How to use JSoup function for HTML parsing in Java

WBOY
Release: 2023-06-26 13:41:33
Original
1547 people have browsed it

HTML is the basic representation of web pages. If you want to obtain and manipulate the content of an HTML document in Java, you need to use an open source parsing tool, such as the JSoup function. JSoup is a Java library for processing HTML documents. It provides a very simple way to extract specific data and elements from HTML documents. This article will introduce the use of JSoup in Java.

  1. Import JSoup

First, you need to import the JSoup library in your Java project. You can find and download JSoup in the Maven central repository. After the download is complete, unzip the JSoup.jar file and import it into your Java project.

  1. Create JSoup's Document object

In your Java code, create a Document object for reading and parsing HTML documents. You can use the connect() method in the JSoup library to specify the URL path to be parsed, and JSoup will automatically download and parse the HTML document and return a Document object, for example:

String url = "https://www.example.com";
Document doc = Jsoup.connect(url).get();
Copy after login
  1. Extract HTML elements

With the Document object, specific HTML elements can be extracted from the HTML document. You can use the select() method of the Document object and pass in the CSS selector syntax to filter out the elements you want, for example:

Element elementById = doc.select("#example-id").first();
Elements elementsByClass = doc.select(".example-class");
Copy after login

The above code obtains the corresponding elements through ID and class name respectively. . You can also use other selectors such as tag name, attribute value, or attribute name.

  1. Manipulating HTML Elements

Once you have obtained the corresponding elements from the HTML document, you can further manipulate these elements. You can use the text() method of the Element object to get the text content of the element, for example:

String textById = elementById.text();
String textByClass = elementsByClass.text();
Copy after login

You can also use the attributes() method of the Element object to get the attribute values ​​of the element.

Element element = doc.select("a").first();
String href = element.attributes().get("href");
Copy after login
  1. Processing HTML forms

In addition to obtaining HTML elements, JSoup also provides convenient tools to process HTML forms. You can use methods in the Document object, such as select(), attr() and text() methods to modify the values ​​of form elements.

Document doc = Jsoup.parse("<form action='/my-url' method='post'>"
        + "<input type='text' name='username'/>"
        + "<input type='password' name='password'/>"
        + "<input type='submit' value='Login'/>"
        + "</form>");

Element formElement = doc.select("form").first();
formElement.attr("action", "login.jsp");
formElement.select("[name=username]").attr("value", "myUsername");
formElement.select("[name=password]").attr("value", "myPassword");
Copy after login
  1. Error handling

When you use JSoup to parse HTML documents, sometimes exceptions may occur. Therefore, in your Java code, you need to handle the IOException and ParseException exceptions thrown by JSoup. These exceptions can be caught using Try-catch blocks.

try {
    Document doc = Jsoup.connect("http://example.com").get();
} catch (IOException e) {
    e.printStackTrace();
} catch (ParseException e) {
    e.printStackTrace();
}
Copy after login

Summary

The above is the method of using JSoup function for HTML parsing. Through JSoup, you can easily obtain and operate HTML documents in Java. By including the JSoup library in your Java project and correctly using JSoup's Document objects, selectors, and APIs for manipulating elements, you can easily complete tasks such as fetching data and modifying HTML forms.

The above is the detailed content of How to use JSoup function for HTML parsing in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template