Android parsing HTML Web page data the first method Jsoup (i)

Source: Internet
Author: User

Some of the things that have recently been found to be boring are crawling data on a Web page, then displaying it using the native code of Android, or borrowing Web data to display it in a custom view.

Get and parse data with the Jsoup-1.10.2.jar library. (Jsoup Baidu Cloud: Http://pan.baidu.com/s/1nvSFKyl)

Jsoup Official documents:
https://jsoup.org/cookbook/

Chinese documents:
http://www.open-open.com/jsoup/

Application Scenarios:

I need to get the data on the Blog Park Web page, the tagged text title, the link, the text introduction.

1, browser open the Web page, and then right-click to view the source page, or press F12 review elements. (difference: the review element (or use the Developer tool, Firebug) to see is now real-time content (after JS modification), and the Web source code see is the first browser received HTTP response content)

Find the corresponding HTML code.

2, find the corresponding node <div class= "Post_item_body" > <a class= "Titlelnk" > <p class= "Post_item_summary"; Use Jsoup parsing. The code is as follows:

 Public voidTestjsoup () {NewThread (NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method Stub                Try{Document doc= Jsoup.connect ("http://www.cnblogs.com/"). get (); Elements Elements= Doc.select ("Div.post_item_body");  for(Element element:elements) {elements title= Element.select ("A.titlelnk"); LOG.E ("Title:", Title.get (0). text ()); LOG.E ("url", Title.get (0). attr ("href")); Elements content= Element.select ("P.post_item_summary"); LOG.E ("Content:", Content.get (0). text ()); }                                    } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(Exception e) {//Todo:handle Exception}} ). Start (); }

3, print the log, as follows:

Here, the Web page data can be basically parsed out.

Android parsing HTML Web page data the first method Jsoup (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.