java多線程抓取鈴聲多多官網的鈴聲數據。本站提示廣大學習愛好者:(java多線程抓取鈴聲多多官網的鈴聲數據)文章只能為提供參考,不一定能成為您想要的結果。以下是java多線程抓取鈴聲多多官網的鈴聲數據正文
一向想演習下java多線程抓取數據。
有天被我發明,鈴聲多多的官網(http://www.shoujiduoduo.com/main/)有年夜量的數據。
經由過程不雅察他們前端獲得鈴聲數據的ajax
http://www.shoujiduoduo.com/ringweb/ringweb.php?type=getlist&listid={種別ID}&page={分頁頁碼}
很輕易就可以發明經由過程轉變 listId和page就可以從辦事器獲得鈴聲的json數據, 經由過程解析json數據,
可以看到都帶有{"hasmore":1,"curpage":1}如許子的指導,經由過程斷定hasmore的值,決議能否停止下一頁的抓取。
然則經由過程下面這個鏈接前往的json中不帶有鈴聲的下載地址
很快便可以發明,點擊頁面的“下載”會看到
經由過程上面的要求,便可以獲得鈴聲的下載地址了
http://www.shoujiduoduo.com/ringweb/ringweb.php?type=geturl&act=down&rid={鈴聲ID}
所以,他們的數據是很輕易被偷的。因而我就開端...
源碼曾經發在github上。假如感興致的童鞋可以檢查
github:https://github.com/yongbo000/DuoduoAudioRobot
上代碼:
package me.yongbo.DuoduoRingRobot; import java.io.BufferedReader; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.Iterator; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.谷歌.gson.Gson; import com.谷歌.gson.JsonArray; import com.谷歌.gson.JsonElement; import com.谷歌.gson.JsonParser; /* * @author yongbo_ * @created 2013/4/16 * * */ public class DuoduoRingRobotClient implements Runnable { public static String GET_RINGINFO_URL = "http://www.shoujiduoduo.com/ringweb/ringweb.php?type=getlist&listid=%1$d&page=%2$d"; public static String GET_DOWN_URL = "http://www.shoujiduoduo.com/ringweb/ringweb.php?type=geturl&act=down&rid=%1$d"; public static String ERROR_MSG = "listId為 %1$d 的Robot產生毛病,已主動停滯。以後page為 %2$d";public static String STATUS_MSG = "開端抓取數據,以後listId: %1$d,以後page: %2$d"; public static String FILE_DIR = "E:/RingData/";public static String FILE_NAME = "listId=%1$d.txt";private boolean errorFlag = false;private int listId;private int page; private int endPage = -1;private int hasMore = 1; private DbHelper dbHelper; /** * 結構函數 * @param listId 菜單ID * @param page 開端頁碼 * @param endPage 停止頁碼 * */ public DuoduoRingRobotClient(int listId, int beginPage, int endPage) {this.listId = listId;this.page = beginPage;this.endPage = endPage;this.dbHelper = new DbHelper();} /** * 結構函數 * @param listId 菜單ID * @param page 開端頁碼 * */ public DuoduoRingRobotClient(int listId, int page) {this(listId, page, -1);} /** * 獲得鈴聲 * */public void getRings() {String url = String.format(GET_RINGINFO_URL, listId, page);String responseStr = httpGet(url);hasMore = getHasmore(responseStr); page = getNextPage(responseStr); ringParse(responseStr.replaceAll("\\{\"hasmore\":[0-9]*,\"curpage\":[0-9]*\\},", "").replaceAll(",]", "]"));}/** * 提議http要求 * @param webUrl 要求銜接地址 * */public String httpGet(String webUrl){URL url;URLConnection conn;StringBuilder sb = new StringBuilder();String resultStr = "";try {url = new URL(webUrl);conn = url.openConnection();conn.connect();InputStream is = conn.getInputStream();InputStreamReader isr = new InputStreamReader(is);BufferedReader bufReader = new BufferedReader(isr);String lineText;while ((lineText = bufReader.readLine()) != null) {sb.append(lineText);}resultStr = sb.toString();} catch (Exception e) {errorFlag = true;//將毛病寫入txtwriteToFile(String.format(ERROR_MSG, listId, page));}return resultStr;}/** * 將json字符串轉化成Ring對象,並存入txt中 * @param json Json字符串 * */public void ringParse(String json) {Ring ring = null;JsonElement element = new JsonParser().parse(json);JsonArray array = element.getAsJsonArray();// 遍歷數組Iterator<JsonElement> it = array.iterator(); Gson gson = new Gson();while (it.hasNext() && !errorFlag) {JsonElement e = it.next();// JsonElement轉換為JavaBean對象ring = gson.fromJson(e, Ring.class);ring.setDownUrl(getRingDownUrl(ring.getId()));if(isAvailableRing(ring)) {System.out.println(ring.toString()); //可選擇寫入數據庫照樣寫入文本//writeToFile(ring.toString());writeToDatabase(ring);}}} /** * 寫入txt * @param data 字符串 * */public void writeToFile(String data) {String path = FILE_DIR + String.format(FILE_NAME, listId);File dir = new File(FILE_DIR);File file = new File(path);FileWriter fw = null;if(!dir.exists()){dir.mkdirs(); }try {if(!file.exists()){file.createNewFile();}fw = new FileWriter(file, true); fw.write(data);fw.write("\r\n");fw.flush();} catch (IOException e) { // TODO Auto-generated catch blocke.printStackTrace(); }finally {try {if(fw != null){fw.close();}} catch (IOException e) { // TODO Auto-generated catch blocke.printStackTrace();}}}/** * 寫入數據庫 * @param ring 一個Ring的實例 * */ public void writeToDatabase(Ring ring) {dbHelper.execute("addRing", ring);} @Overridepublic void run() {while(hasMore == 1 && !errorFlag){if(endPage != -1){if(page > endPage) { break; }}System.out.println(String.format(STATUS_MSG, listId, page)); getRings();System.out.println(String.format("該頁數據寫入完成"));}System.out.println("ending...");} private int getHasmore(String resultStr){Pattern p = Pattern.compile("\"hasmore\":([0-9]*),\"curpage\":([0-9]*)"); Matcher match = p.matcher(resultStr); if (match.find()) { return Integer.parseInt(match.group(1)); } return 0; } private int getNextPage(String resultStr){Pattern p = Pattern.compile("\"hasmore\":([0-9]*),\"curpage\":([0-9]*)");Matcher match = p.matcher(resultStr);if (match.find()) {return Integer.parseInt(match.group(2));}return 0;} /** * 斷定以後Ring能否知足前提。當Ring的name年夜於50個字符或是duration為小數則不相符前提,將被剔除。 * @param ring 以後Ring對象實例 * */private boolean isAvailableRing(Ring ring){Pattern p = Pattern.compile("^[1-9][0-9]*$"); Matcher match = p.matcher(ring.getDuration()); if(!match.find()){return false;}if(ring.getName().length() > 50 || ring.getArtist().length() > 50 || ring.getDownUrl().length() == 0){return false;}return true;} /** * 獲得鈴聲的下載地址 * @param rid 鈴聲的id * */ public String getRingDownUrl(String rid){String url = String.format(GET_DOWN_URL, rid); String responseStr = httpGet(url);return responseStr;}}