最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java多線程抓取鈴聲多多官網(wǎng)的鈴聲數(shù)據(jù)

 更新時間:2016年04月28日 11:50:47   作者:bobo_ll  
很容易就能發(fā)現(xiàn)通過改變 listId和page就能從服務(wù)器獲取鈴聲的json數(shù)據(jù), 通過解析json數(shù)據(jù), 可以看到都帶有{"hasmore":1,"curpage":1}這樣子的指示,通過判斷hasmore的值,決定是否進行下一頁的抓取。 但是通過上面這個鏈接返回的json中不帶有鈴聲的下載地址

一直想練習下java多線程抓取數(shù)據(jù)。

有天被我發(fā)現(xiàn),鈴聲多多的官網(wǎng)(http://www.shoujiduoduo.com/main/)有大量的數(shù)據(jù)。

通過觀察他們前端獲取鈴聲數(shù)據(jù)的ajax


http://www.shoujiduoduo.com/ringweb/ringweb.php?type=getlist&listid={類別ID}&page={分頁頁碼}

很容易就能發(fā)現(xiàn)通過改變 listId和page就能從服務(wù)器獲取鈴聲的json數(shù)據(jù), 通過解析json數(shù)據(jù),

可以看到都帶有{"hasmore":1,"curpage":1}這樣子的指示,通過判斷hasmore的值,決定是否進行下一頁的抓取。

但是通過上面這個鏈接返回的json中不帶有鈴聲的下載地址

很快就可以發(fā)現(xiàn),點擊頁面的“下載”會看到

通過下面的請求,就可以獲取鈴聲的下載地址了

http://www.shoujiduoduo.com/ringweb/ringweb.php?type=geturl&act=down&rid={鈴聲ID}


所以,他們的數(shù)據(jù)是很容易被偷的。于是我就開始...

源碼已經(jīng)發(fā)在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.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.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發(fā)生錯誤,已自動停止。當前page為 %2$d";public static String STATUS_MSG = "開始抓取數(shù)據(jù),當前l(fā)istId: %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;
/** * 構(gòu)造函數(shù) * @param listId 菜單ID * @param page 開始頁碼 * @param endPage 結(jié)束頁碼 * */
public DuoduoRingRobotClient(int listId, int beginPage, int endPage)
 {this.listId = listId;this.page = beginPage;this.endPage = endPage;this.dbHelper = new DbHelper();}
/** * 構(gòu)造函數(shù) * @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(",]", "]"));}/** * 發(fā)起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字符串轉(zhuǎn)化成Ring對象,并存入txt中 * @param json Json字符串 * */public void ringParse(String json) {Ring ring = null;JsonElement element = new JsonParser().parse(json);JsonArray array = element.getAsJsonArray();// 遍歷數(shù)組Iterator<JsonElement> it = array.iterator();
Gson gson = new Gson();while (it.hasNext() && !errorFlag) {JsonElement e = it.next();// JsonElement轉(zhuǎn)換為JavaBean對象ring = gson.fromJson(e, Ring.class);ring.setDownUrl(getRingDownUrl(ring.getId()));if(isAvailableRing(ring)) {System.out.println(ring.toString());
//可選擇寫入數(shù)據(jù)庫還是寫入文本//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();}}}/** * 寫入數(shù)據(jù)庫 * @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("該頁數(shù)據(jù)寫入完成"));}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為小數(shù)則不符合條件,將被剔除。 * @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;}}

相關(guān)文章

  • SpringBoot中自定義首頁(默認頁)及favicon的方法

    SpringBoot中自定義首頁(默認頁)及favicon的方法

    這篇文章主要介紹了SpringBoot中如何自定義首頁(默認頁)及favicon,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • java收集器Collector案例匯總

    java收集器Collector案例匯總

    這篇文章主要介紹了java收集器Collector案例匯總,Collectors作為Stream的collect方法的參數(shù),Collector是一個接口,它是一個可變的匯聚操作,更多相關(guān)介紹,需要的朋友可以參考下
    2022-06-06
  • java實現(xiàn)gif動畫效果(java顯示動態(tài)圖片)

    java實現(xiàn)gif動畫效果(java顯示動態(tài)圖片)

    這篇文章主要介紹了java實現(xiàn)gif動畫效果示例(java顯示動態(tài)圖片),需要的朋友可以參考下
    2014-04-04
  • java實現(xiàn)紙牌游戲之小貓釣魚算法

    java實現(xiàn)紙牌游戲之小貓釣魚算法

    這篇文章主要為大家詳細介紹了java實現(xiàn)紙牌游戲之小貓釣魚算法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • java遞歸算法實例分析

    java遞歸算法實例分析

    這篇文章主要介紹了java遞歸算法實例分析,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • SpringBoot實現(xiàn)異步事件Event詳解

    SpringBoot實現(xiàn)異步事件Event詳解

    這篇文章主要介紹了SpringBoot實現(xiàn)異步事件Event詳解,異步事件的模式,通常將一些非主要的業(yè)務(wù)放在監(jiān)聽器中執(zhí)行,因為監(jiān)聽器中存在失敗的風險,所以使用的時候需要注意,需要的朋友可以參考下
    2023-11-11
  • 最新評論

    玉树县| 道真| 客服| 宁南县| 迁安市| 义马市| 和政县| 海盐县| 平阴县| 铜山县| 宁安市| 棋牌| 个旧市| 革吉县| 扶风县| 新野县| 葵青区| 建水县| 溧阳市| 华容县| 行唐县| 哈巴河县| 白银市| 民权县| 秭归县| 承德县| 卢湾区| 秦皇岛市| 余庆县| 陇川县| 岳阳市| 隆德县| 芜湖市| 顺昌县| 出国| 综艺| 灵璧县| 文昌市| 建宁县| 北票市| 阿鲁科尔沁旗|