Jsoup series Learning (2)-parsing html files,
Parse html files
1. When we send an http request, sometimes the returned result is an html string. You need to obtain and parse an HTML document from a website and search for relevant data. You can use the following solution:
UseJsoup.connect(String url)Method:
// Send the request Document doc = Jsoup. connect ("https://www.baidu.com /"). get (); // get the control Element content = doc with the ID number of kw. getElementById ("kw"); // all attributes of the output control System. out. println (content. attributes ());
Output result
Id = "kw" name = "wd" class = "s_ipt" value = "" maxlength = "255" autocomplete = "off"
The page source code shows that it is consistent with the output result, which is like the selenium automation framework.Driver. findElement (By. id ("kw "))Find the control and perform various operations on the heap space.
Description
connect(String url) Method to create a new Connection,And get()Obtain and parse an HTML file. If an error occurs when retrieving HTML from the URL, an IOException is thrown and should be handled as appropriate.
ConnectionThe interface also provides a method chain to solve special requests, as follows:
Document doc = Jsoup.connect("http://example.com") .data("query", "Java") .userAgent("Mozilla") .cookie("auth", "token") .timeout(3000) .post();Data extraction 1. Use the DOM method to traverse a document Object
Since we want to get the specified data from html, we need to first find the control, we call it the Elements object, and then obtain some values of the control.
- Search Element
GetElementById (String id)
GetElementsByTag (String tag) tag Name Search
GetElementsByClass (String className) class Name Search
getElementsByAttribute(String key)(And related methods)
- Element siblings:
siblingElements(),firstElementSibling(),lastElementSibling();nextElementSibling(),previousElementSibling()
- Graph:
parent(),children(),child(int index)
Element Data
attr(String key)Get attributesattr(String key, String value)Set attributes
attributes()Get all attributes
id(),className()AndclassNames()
text()Get Text Contenttext(String value)Set text content
html()Obtain HTML from Elementhtml(String value)Set HTML content in the element
outerHtml()Obtain HTML content outside the element
data()Get data content (for example, script and style labels)
tag()AndtagName()
Operate HTML and text
append(String html),prepend(String html)
appendText(String text),prependText(String text)
appendElement(String tagName),prependElement(String tagName)
html(String value)
Example 1: Get the control name
1 // send the request 2 Document doc = Jsoup. connect ("https://www.baidu.com /"). get (); 3 // get all element objects of class name s_ipt 4 Elements element = doc. getElementsByClass ("s_ept"); 5 // get the name attribute value of the first element object 6 String value = element. get (0 ). attr ("name"); 7 System. out. println (value );
Example 2: If there is no id or name element, you can only search by relative path.
1 // send the request 2 Document doc = Jsoup. connect ("https://www.baidu.com /"). get (); 3 // obtain the first sub-Element 4 element = doc with the id of u1. getElementById ("u1 "). child (0); 5 // obtain the element link 6 String value1 = element. attr ("href"); 7 // get the element text content 8 String value2 = element. text ();
Output result:
Http://news.baidu.com news
Reference
1. jsoup learning Summary: http://blog.csdn.net/u010814849/article/details/52526582
2. http://www.open-open.com/jsoup/load-document-from-url.htm
3. jsoup learning Summary: http://www.cnblogs.com/tomcattd/archive/2013/01/02/2842137.html