私はいつもJava Multi-Threadingデータを練習したいと思っていました。
ある日、Rings Duoduo(http://www.shoujiduoduo.com/main/)の公式ウェブサイトには大量のデータがあることがわかりました。
フロントエンドAjaxを観察して着メロデータを取得します
http://www.shoujiduoduo.com/ringweb/ringweb.php?type=getList&listid= {category id}&page = {pagination page番号}}
ListIDとページを変更することで、サーバーから着信音のJSONデータを取得し、JSONデータを解析することで簡単に見つけることができます。
{"hasmore":1、 "curpage":1}のような指示があることがわかります。 Hasmoreの価値を判断することにより、次のページをクロールするかどうかを決定します。
ただし、jsonの上記のリンクから返されたJSONのダウンロードアドレスは、JSONでは利用できません
ページの「ダウンロード」をクリックして、すぐに表示されることがわかります。
次のリクエストを通じて、着信音のダウンロードアドレスを取得できます
http://www.shoujiduoduo.com/ringweb/ringweb.php?type=geturl&act=down&rid= {ringtone id}
したがって、彼らのデータは簡単に盗まれます。だから私は始めました...
ソースコードはGitHubに投稿されています。子供の靴に興味があるなら、チェックしてください
github:https://github.com/yongbo000/duoduoaudiorobot
コード:
packy.yongbo.duoduoringrobot; import java.io.bufferedreader; import java.io.file; import java.io.filewriter; Import java.io.ioexception; Impot java.io.inputStream; Import java.io.inputStreamReader; Import.net.net.net.url.urlcnect.url. java.util.iterator; Import java.util.regex.matcher; Import java.util.regex.pattern; Import com.google.gson.jsonarray; Import com.google.gson.jsonelement; import com.google.gson.gson.jongsparser; 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&page = apage = %2$dddo" "http://www.shoujiduoduo.com/ringweb/ringweb.php?type=geturl&act=down&rid=%1$d";パブリック静的文字列error_msg =" "エラーは%1 $ dのリストで発生しました。 %1 $ d、現在のページ:%2 $ d "; public static string file_dir =" e:/ringdata/"; public static string file_name =" listid =%1 $ d.txt "; private boolean errorflag = false; private int page; private int endpage = -1; private int hasmore; @paramページ開始ページ番号* @paramエンドページエンドページ番号**/public duoduoringRobotclient(int listid、int beginpage、int endpage){this.listid = fistid; this.page = beginpage; this.endpage; */public DuoduoringRobotClient(int listid、int page){this(listid、page、-1);}/** * get ringtone * * */public void getrings(){string url = string.format(get_ringinfo_url、listid、page); string resspentesthers = httpget(url); hasmore = gethasmore(sponess(sonsponese); getNextPage(Respondester); RingParse(Respondestr.replaceall( "// {/" hasmore/":[0-9]*、/" Curpage/":[0-9]* //}、"、 ").replaceall("、] "、"] ");} 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.getutintintStream is -new -new -newedis(); inputStreamReader(IS); BufferedReader bufreader = new BufferedReader(ISR); String Linetext; while((linetext = bufreader.readline()!= null){sb.append(linetext);} resultStr = sb.toString();} catch(erryflag = true; // txtwriteTofileにエラーを書きます(string.format(error_msg);そして、txt* @param json json string**/public void ringparse(string json){ring ring = null; jsonelement element = new jsonparser()。parse(json); jsonarray array = element.getasjsonarray(); // raverse the raverse the raverse the raverse the raverse the raverse <jsonelement> gson (it.hasnext()&&!errorflag){jsonelement e = it.next(); // jsonelementをjavabean object ring = gson.fromjson(e、ring.class); ring.setdownurl(getringdownurl(ring.getid()); if(isavailablering(ring)); {system.out.println(ring.toString()); //データベースまたはテキストに書き込むか、// writeTofile(ring.toString()); writeTodatabase(); writeTodatabase(ring);}}/** * txt * @param Data String */public void writeTodile(wrinteTofile(sting suring string(data))/** * string.format(file_name、listId); file dir = new file(file_dir); file file = new file(path); filewriter fw = null; if(!dir.mkdirs();} try {if(!file.exists()){file.createNewfile(); true); fw.write(data); fw.write( "/r/n"); fw.flush();} catch(ioexception e){// dodo auto-feenated catch blocke.printstacktrace();}最後に{try {if(fw!= null){fw.clsection excepsection catch blocke.printstacktrace();}}}/*** @paramリングに書き込み* @paramリングリングのインスタンス**/public void writetodatabase(リングリング){dbhelper.execute( "addring"、ring);} @overridebublic void ron() !errorflag){if(endpage!= -1){if(page> endpage){break; }} system.out.println(string.format(status_msg、listid、page)); getrings(); system.out.println( "string.format("このページに記述されたデータが完了しました "); 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.pirt.find();現在のリングが条件を満たしているかどうかを判断します。リング名が50文字を超えている場合、または期間が小数である場合、条件を満たしておらず、削除されます。 * @Param Ring Current Ring Object Instance**/private Boolean isavailablering(リングリング){パターンp = pattern.compile( "^[1-9] [0-9]* $"); matcher match = p.matcher(ring.getDuration()); if(!match.find()){return false; || ring.getdownurl()。length()== 0){return false;}/** * @param rid rid rid rid in rid string getringdownurl(string){string url = string.format(get_down_url、rid = httpget(url;}}