Catch jsoup_01_ program code

Source: Internet
Author: User

1. Choice of Scheme:

1.1. httpclient Library to get raw JSON data

1.2. JSON library Gets the HTML content we need

1.3. use Jsoup to parse the HTML content we obtained

2, not directly use jsoup, reason:

2.1, it will automatically complement the entire HTML header and tail (

Processing method: manually specify Parser.xmlparser ()

2.2, If the attribute is not wrapped in 2 double quotation marks, It will be the 2 double quotation marks complete ... This function cannot be closed in jsoup ...

3. Sample Code:

3.1. Tool Class

 packagez_utils;Importorg.apache.http.HttpResponse;Importorg.apache.http.HttpStatus;Importorg.apache.http.client.methods.HttpGet;Importorg.apache.http.client.methods.HttpPost;Importorg.apache.http.entity.StringEntity;Importorg.apache.http.impl.client.DefaultHttpClient;Importorg.apache.http.util.EntityUtils; public classtzhttpclient{ public Static voidMain (string[] Args)throwsException {String Strrtn=Postz ("http://ajax.mianbao99.com/vod-showlist-id-8-order-time-c-3719-p-2.html",            NULL,            true);    System.out.println (strrtn); }    // ***@SuppressWarnings ("deprecation")     public StaticString Postz (string _strurl, string _strparam,Boolean_bneedresponse)throwsException {//POST request returns resultsDefaulthttpclient httpClient =Newdefaulthttpclient (); HttpPost Method=NewHttpPost (_strurl); if(NULL!=_strparam) {            //solve Chinese garbled problemstringentity entity =NewStringentity (_strparam, "utf-8"); Entity.setcontentencoding ("UTF-8"); Entity.setcontenttype ("application/json");        Method.setentity (entity); } HttpResponse result=Httpclient.execute (method); /**the request was sent successfully and received a response **/        if(result.getstatusline (). getstatuscode () = = 200)        {            if(!_bneedresponse)return NULL; String Str=entityutils.tostring (result.getentity ()); //System.out.println (str);            returnstr; }        return NULL; } @SuppressWarnings ("deprecation")     public StaticString GetZ (string _strurl)throwsException {defaulthttpclient Client=Newdefaulthttpclient (); //send a GET requestHttpGet request =NewHttpGet (_strurl); HttpResponse Response=Client.execute (request); /**the request was sent successfully and received a response **/        if(response.getstatusline (). getstatuscode () = =Httpstatus.sc_ok) {            /**read JSON string data returned by the server **/String strresult=entityutils.tostring (response.getentity ()); //System.out.println (strresult);            returnstrresult; } System.out.println ("get Request Submission failed:" +_strurl); return NULL; }}

3.2. Test function code

 packagetest;Importorg.jsoup.Connection;///import org.jsoup.Connection;Importorg.jsoup.Jsoup;Importorg.jsoup.nodes.*;Importorg.jsoup.parser.Parser;Importnet.sf.json.*;ImportZ_utils. tzhttpclient; public classttest01{ public Static voidMain (string[] Args)throwsException {String strhtml= Tzhttpclient.getz ("http://ajax.mianbao99.com/vod-showlist-id-8-order-time-c-3719-p-2.html"); Jsonobject Jsoupobj=Jsonobject.fromobject (strhtml); if(! Jsoupobj.containskey ("ajaxtxt"))            return; //Connection conn = null;//conn.parser (parser.xmlparser ());String strajaxtxt = jsoupobj.getstring ("ajaxtxt"); Document Doc= Jsoup.parse (strajaxtxt, "", Parser.xmlparser ());    System.out.println (doc.html ()); }}

4.

Catch jsoup_01_ program code

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.