標籤:
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_方案代碼