抓jsoup_01_方案代碼

來源:互聯網
上載者:User

標籤:

1、方案選擇:

  1.1、HttpClient庫 擷取 原始的 json資料

  1.2、JSON庫 取得 我們需要的HTML內容

  1.3、使用 jsoup 解析 我們取得的HTML內容

 

2、不直接使用 jsoup,原因:

  2.1、它會自動補全 HTML的頭和尾(<html/><body/>等),jsoup中沒有這個

    處理方法:手動指定 Parser.xmlParser()

  2.2、如果屬性 沒有用 2個雙引號包裹起來,它會將 這2個雙引號補全... 這個功能 在jsoup裡面沒法關閉...

 

3、範例程式碼:

  3.1、工具類

package z_utils;import org.apache.http.HttpResponse;import org.apache.http.HttpStatus;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;public class TzHttpClient{    public static void main(String[] args) throws Exception    {        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 static String PostZ(String _strUrl, String _strParam, boolean _bNeedResponse) throws Exception    {        //post請求返回結果        DefaultHttpClient httpClient = new DefaultHttpClient();        HttpPost method = new HttpPost(_strUrl);        if (null != _strParam)        {            //解決中文亂碼問題            StringEntity entity = new StringEntity(_strParam, "utf-8");            entity.setContentEncoding("UTF-8");            entity.setContentType("application/json");            method.setEntity(entity);        }        HttpResponse result = httpClient.execute(method);        /**請求發送成功,並得到響應**/        if (result.getStatusLine().getStatusCode() == 200)        {            if (! _bNeedResponse)                return null;            String str = EntityUtils.toString(result.getEntity());            //System.out.println(str);            return str;        }        return null;    }        @SuppressWarnings("deprecation")    public static String GetZ(String _strUrl) throws Exception    {        DefaultHttpClient client = new DefaultHttpClient();        //發送get請求        HttpGet request = new HttpGet(_strUrl);        HttpResponse response = client.execute(request);        /**請求發送成功,並得到響應**/        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)        {            /**讀取伺服器返回過來的json字串資料**/            String strResult = EntityUtils.toString(response.getEntity());            //System.out.println(strResult);            return strResult;        }        System.out.println("get請求提交失敗:" + _strUrl);        return null;    }}

  3.2、測試 功能代碼

package test;import org.jsoup.Connection;///import org.jsoup.Connection;import org.jsoup.Jsoup;import org.jsoup.nodes.*;import org.jsoup.parser.Parser;import net.sf.json.*;import z_utils.TzHttpClient;public class Ttest01{    public static void main(String[] args) throws Exception    {        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、

 

抓jsoup_01_方案代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.