国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - java多線程抓取鈴聲多多官網的鈴聲數據

java多線程抓取鈴聲多多官網的鈴聲數據

2020-04-21 11:43bobo_ll JAVA教程

很容易就能發現通過改變 listId和page就能從服務器獲取鈴聲的json數據, 通過解析json數據, 可以看到都帶有{"hasmore":1,"curpage":1}這樣子的指示,通過判斷hasmore的值,決定是否進行下一頁的抓取。 但是通過上面這個鏈接返回的json中不

一直想練習下java多線程抓取數據。

有天被我發現,鈴聲多多的官網(http://www.shoujiduoduo.com/main/)有大量的數據。

通過觀察他們前端獲取鈴聲數據的ajax

java多線程抓取鈴聲多多官網的鈴聲數據

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}

java多線程抓取鈴聲多多官網的鈴聲數據

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

源碼已經發在github上。如果感興趣的童鞋可以查看

github:https://github.com/yongbo000/DuoduoAudioRobot

上代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<pre class="brush:java;">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發生錯誤,已自動停止。當前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;}}</pre>

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 在线视频成人 | 午夜黄色影院 | 久久亚洲视频 | 凹凸日日摸日日碰夜夜爽孕妇 | 成人免费黄色毛片 | 欧美在线不卡视频 | 亚洲va欧美va人人爽成人影院 | 成人超碰在线 | 黄视频入口 | 麻豆精品国产91久久久久久 | 免费成人在线网站 | 国产精品免费在线 | av国产精品 | 嫩草在线视频 | 毛片在线网址 | 黄色大片在线播放 | 精品欧美乱码久久久久久1区2区 | 中文字幕不卡一区 | 黄色在线免费观看 | 日韩精品免费在线视频 | 国产在线一区二区三区 | 精品国产乱码久久久久久闺蜜 | 老司机午夜影院 | 亚洲精品视频播放 | 91久久久久久久久 | 综合久久亚洲 | 久久精品中文 | 亚洲欧美日韩国产综合精品二区 | 亚洲一本 | 黄色毛片视频网站 | 全部古装三级在线播放 | 欧美激情一区二区 | 日韩精品一区二区三区中文字幕 | 久久精品视频免费观看 | 成人自拍视频 | 99re热精品视频 | the蜜臀av入口 | 中文字幕人成乱码在线观看 | 欧美成年黄网站色视频 | 国产精品毛片久久久久久久 | 久久三区 |