この記事では、以前のすべての機能をクロールするJavaの実装について説明します。次のように、参照のために共有してください。
夢があるに違いない、それが実現されたらどうしますか? shuangseボールを頻繁に購入する友人と私は質問があると信じています。 2番目のカラーボールが前の期間に一等賞を受賞したかどうかが好きですか?勝った場合、私は基本的にこの賭けをあきらめることができます。なぜなら、歴史の中で2秒の2つのカラーボールに2つの連続した引き分けはないはずだからですよね?では、以前の引き分けの結果は何でしたか?私はJavaでシンプルなクラスを書き、ダブルカラーボールの宝くじのすべての結果をcraうとしました。私はもともとAndroidバージョンを開発したかったのですが、UIや他のことは準備に時間がかかるので、チャンスがあればもう一度開発します。
java.io.bufferedreader; Import java.io.bufferedwriter; import java.io.file; Import java.io.filewriter; Import java.io.ioexception; Import java.io.inputStream; Import java.io.inputStreamextion; Import.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net. java.util.regex.matcher; Import java.util.regex.pattern; Import java.util.zip.gzipinputStream; public class allballs {private static stringbuffer mstringbuffer; public static void main(string [] args){system.out.println( "geting ..."); mstringbuffer = new StringBuffer(); string baseurlprefix = "http://kaijiang.zhcw.com/zhcw/html/ssq/list_"; string baseurlsuffix = ".html"; string homeurl = "http://kaijiang.zhcw.com/zhcw/html/ssq/list_1.html"; string pagecountcontent = gethtmlstring(homeurl); int pagecount = getPageCount(pagecountContent); if(pagecount> 0){for(int i = 1; i <= pagecount; i ++){string url = baseurlprefix + i + baseurlsuffix; string pagecontent = gethtmlstring(url); if(pagecontent!= null &&!pagecontent.equals( "")){getoneTermContent(pagecontent); } else {system.out.println( "thread" + i + "page lost"); } try {thread.sleep(1200); } catch(Exception e){// todo:handle例外}} file file = new file( "double color ball.txt"); if(file.exists()){file.delete(); } try {filewriter writer = new filewriter(file); BufferedWriter BufferedWriter = new BufferedWriter(Writer); bufferedwriter.write(mstringbuffer.toString()); bufferedwriter.close(); writer.close(); } catch(ioException e){// todo auto-fenated catch block e.printstacktrace(); } // bufferedwriter writer = new bufferedWriter(new outputs)} else {system.out.println( "result pages as as 0"); } system.out.println( "complete!"); }/***ページの総数を取得* @param result*/private static int getPageCount(string reghx = "// d+/"> last page "; pattern pattern = pattern.compile(regex); matcher matcher = pattern.matcher(result); result [] splits = null; content.split( "/" ");壊す; } if(splits!= null && splits.length == 2){string countString = splits [0]; if(countString!= null &&!countString.equals( "")){return integer.parseint(countstring); }} return 0; } / *** Webページソースコードを取得* @return* / private static string gethtmlstring(string targeturl){string content = null; httpurlconnection connection = null; try {url url = new url(targeturl); connection =(httpurlconnection)url.openconnection(); connection.setRequestMethod( "post"); connection.setRequestProperty( "user-agent"、 "mozilla/4.0(互換; msie 7.0; Windows 7)"); connection.setRequestProperty( "Accept"、 "image/gif、image/x-xbitmap、image/jpeg、image/pjpeg、application/x-shockwave-flash、application/vnd.ms-powerpoint、application/vnd.ms-excel、application/msword、 */ *"); connection.setRequestProperty( "Accept-Language"、 "zh-cn"); connection.setRequestProperty( "ua-cpu"、 "x86"); //なぜデフレートがないのですか? connection.setRequestProperty( "Accept-Encoding"、 "gzip"); connection.setRequestProperty( "content-type"、 "text/html"); //キープアライブ、何が用途ですか?あなたはウェブサイトにアクセスしていません、あなたはそれを収集しています。ちょっとハイ。他人への圧力を和らげることも、自分自身を減らすことです。 connection.setRequestProperty( "Connection"、 "close"); //キャッシュを使用しないでください。頻繁にリンクにアクセスしないため、役に立ちません。 (プログラムの場合)connection.setusecaches(false); connection.setConnectTimeout(6 * 1000); connection.setreadTimeout(6 * 1000); connection.setDoOutput(true); connection.setDoinput(true); connection.setRequestProperty( "charset"、 "utf-8"); connection.connect(); if(200 == connection.getResponseCode()){inputstream inputstream = null; if(connection.getContentEncoding()!= null &&!connection.getContentenCoding()。 if(encode!= null &&!encode.equals( "")&& encode.indexof( "gzip")> = 0){inputstream = new gzipinputStream(connection.getInputStream()); }} if(null == inputstream){inputstream = connection.getInputStream(); } bufferedreader reader = new BufferedReader(new inputstreamReader(inputStream、 "utf-8")); stringbuilder builder = new StringBuilder();文字列line = null; while((line = reader.readline())!= null){builder.append(line).append( "/n"); } content = builder.toString(); }} catch(例外e){e.printstacktrace(); }最後に{if(connection!= null){connection.disconnect(); }} return content; } private static void getoneTermContent(string pagecontent){string regex = "<td align =/" center/"style =/" padding-left:10px;/"> [// s // s]+?</em> </td>";パターンパターン= pattern.compile(regex); matcher matcher = pattern.matcher(pagecontent); while(matcher.find()){string oneTermContent = matcher.group(); getoneTermnumbers(oneTermContent); }} private static void getoneTermnumbers(String oneTermContent){string regex = "> // d+<";パターンパターン= pattern.compile(regex); matcher matcher = pattern.matcher(oneTermContent); while(matcher.find()){string content = matcher.group(); string ballnumber = content.substring(1、content.length()-1); mstringbuffer.append(ballnumber).append( ""); } mstringbuffer.append( "/r/n"); }}実行結果:
Java関連のコンテンツの詳細については、このサイトのトピックをご覧ください:「Javaネットワークプログラミングスキルの要約」、「Javaソケットプログラミングスキルの概要」、「Javaファイルとディレクトリ操作スキルの概要」、Javaデータ構造とアルゴリズムに関するチュートリアル」、「Java Operation dom Node operince opertions "summary opers of oper opering" opering opers of "summary" summary of "summary of Java
この記事がみんなのJavaプログラミングに役立つことを願っています。