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.
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.
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();
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");
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.
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();
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");
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");
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(); }
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!