나는 항상 Java 멀티 스레딩 데이터 잡기를 연습하고 싶었습니다.
어느 날 나는 반지의 공식 웹 사이트 Duoduo (http://www.shoujiduoduo.com/main/)가 많은 양의 데이터를 가지고 있음을 발견했습니다.
프론트 엔드 Ajax를 관찰하여 벨소리 데이터를 얻습니다
http://www.shoujiduoduo.com/ringweb/ringweb.php?type=getlist&listid= {Category ID} & page = {Pagination Page Number}
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
코드 :
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; 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;/ * * * * * * * * * * * * * * * * * * * 2013/4/16 * */public class duoduoringRobotclient는 runnable {public static string get_ringinfo_url = "http://www.shoujiduoduo.com/ringweb/ringweb.php?type=getList&listid=%1%1$ d&page=%2$ DD"; "http://www.shoujiduoduo.com/ringweb/ringweb.php?type=geturl&act=down&rid=%1$d";public static string error_msg =" %1 $ d의 ListId에서 오류가 발생했으며 현재 정체 상태가 자동으로 중지되었습니다. Public STRING is IS %DIST : ";" %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 intpage = -1; private intmore = 1; private dbhelper dbhelper;* @par in @partuctor* @parec* @pritucto. @param 페이지 시작 페이지 번호* @param 엔드 페이지 엔드 페이지 번호**/public duoduoringrobotclient (int listid, int beginpage, int endpage) {this.listid = listid; this.page = beginpage; endpage = endpage; this.dbhelper = new dbhelper (}/*** @param listid id id id menu* @param listid menu* @param). */public duoduoringrobotclient (int listid, int page) {this (listid, page, -1);}/** * get ringtone */public void getrings () {String url = string.format (get_ringinfo_url, listid, page); hasmore (has gethmore); getNextPage (responsect); ringparse (responsect.replaceall ( "// {/"hasmore/": [0-9]*,/"Curpage/": [0-9]* //},", "") .replaceall ( ",]", "]);}); httpget (string weburl) {url url; urlconnection conn; stringbuilder sb = new StringBuilder (); String resulttr = "; try {url = new url (weburl); conn = url.openConnection (); conn.connect (); conn.connect (inputStream is = conngeTINTSTREAM); inputStreamReader (is); bufferedReader bufreader = new bufferedReader (ISR); String Linetext; while ((linetext = bufreader.readline ())! = null) {sb.append (linetext);} resulttr = sb.toString ();} catch (exception e) {errorflag = true; // txtWriteTofile (error_ms, listid, page); txt* @param json string*/public void ringparse (String json) {ring ring = null; jsonlement element = new jsonparser (). parse (json); jsonarray array = element.getasjsonArray (); // 배열 반복 <jsonElement> It = gson (gson gson); (it.hasnext () &&! errorflag) {jsonElement e = it.next (); // jsonElement 변환 javabean 객체 ring = gson.fromjson (e, ring.class); ring.setdownurl (getringdownUrl (ring.getId (ring)); {system.out.println (ring.toString ()); // 데이터베이스 또는 텍스트 // writeTofile (ring.toString ()); writeTodatabase (ring);}}}/** * {string writeTofile (string void void) (string path)에 쓰기. 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, file, file, file) true); fw.write (data); fw.write ( "/r/n"); fw.flush ();} catch (ioexception e) {// todo auto-auto-auto-auto-genated catch blocke.printstacktrace ();} 마침내 {if (fw! = null) {fw.close ()}} catch (ioexmence e)}}. 자동 유래 Catch Blocke.printstacktrace ()}}}}}}}}}}}}}}}}}}}}}}}}* @param ring*/public void ring ring) {dbhelper.execute ( "addring", ring); -1) {if (page> endpage) {break; }}} system.out.println (string.format (status_msg, listid, page)); getrings (); system.out.println (String.format ( "이 페이지에 작성된 데이터가 완료되었습니다");} system.out.println ( "ending ..."); pattern.compile ( "/"hasmore/": ([0-9]*),/"Curpage/": ([0-9]*)"); 매치 업체 매치 = p.matcher (resultstr); if (match.find ()) {return integer.parseint (match.group (1)); } return 0;} private int getNextPage (String resulttr) {Pattern p = pattern.compile ( "/"hasmore/": ([0-9]*)*),/"Curpage/": ([0-9]*)"); Matcher Match = p.matcher (resulttr); if (match.find ()) {return intger.parseint (2)); 현재 링이 조건을 충족하는지 여부를 결정하십시오. 링 이름이 50 자 미만이거나 지속 시간이 소수점 인 경우 조건을 충족하지 않으며 제거됩니다. * @param ring current ring 객체 인스턴스*/개인 부울 isavailablering (ring ring) {pattern p = pattern.compile ( "^[1-9] [0-9] [0-9]* $"); matcher match = p.matcher (ring.getDuration ()); if (! match.find ()) {return false; .