Java實現(xiàn)爬取往期所有雙色球開獎結(jié)果功能示例
本文實例講述了Java實現(xiàn)爬取往期所有雙色球開獎結(jié)果功能。分享給大家供大家參考,具體如下:
夢想還是要有的,萬一實現(xiàn)了呢?我相信經(jīng)常買雙色球的朋友和我都會有一個疑問,就是往期雙色球的開獎結(jié)果是什么?我鐘意的這一注雙色球在往期是否開過一等獎,如果開過的話,基本上可以放棄這一注了,因為歷史上應(yīng)該沒有出現(xiàn)過兩期雙色球開獎完全一致的吧?那么往期的開獎結(jié)果是什么呢?我自己用Java寫了一個簡易的類,爬取所有雙色球開獎結(jié)果,本來想開發(fā)安卓版本的,由于UI等需要時間準(zhǔn)備,有緣再開發(fā)吧。
import 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.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import 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("正在獲取..."); 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("第" + i + "頁丟失"); } try { Thread.sleep(1200); } catch (Exception e) { // TODO: handle exception } } File file = new File("雙色球.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-generated catch block e.printStackTrace(); } //BufferedWriter writer = new BufferedWriter(new OutputS) } else { System.out.println("結(jié)果頁數(shù)為0"); } System.out.println("完成!"); } /** * 獲取總頁數(shù) * @param result */ private static int getPageCount(String result) { String regex = "\\d+\">末頁"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(result); String[] splits = null; while (matcher.find()) { String content = matcher.group(); splits = content.split("\""); break; } if (splits != null && splits.length == 2) { String countString = splits[0]; if (countString != null && !countString.equals("")) { return Integer.parseInt(countString); } } return 0; } /** * 獲取網(wǎng)頁源碼 * @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 (compatible; 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"); //為什么沒有deflate呢 connection.setRequestProperty("Accept-Encoding", "gzip"); connection.setRequestProperty("Content-type", "text/html"); //keep-Alive,有什么用呢,你不是在訪問網(wǎng)站,你是在采集。嘿嘿。減輕別人的壓力,也是減輕自己。 connection.setRequestProperty("Connection", "close"); //不要用cache,用了也沒有什么用,因為我們不會經(jīng)常對一個鏈接頻繁訪問。(針對程序) 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().equals("")) { String encode = connection.getContentEncoding().toLowerCase(); 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(); String line = null; while ((line = reader.readLine()) != null) { builder.append(line).append("\n"); } content = builder.toString(); } } catch (Exception e) { e.printStackTrace(); } finally { 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 pattern = 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 pattern = 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"); } }
運(yùn)行結(jié)果:
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java網(wǎng)絡(luò)編程技巧總結(jié)》、《Java Socket編程技巧總結(jié)》、《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
Nacos-SpringBoot框架啟動不加載bootstrap.yml的解決
這篇文章主要介紹了Nacos-SpringBoot框架啟動不加載bootstrap.yml的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11Idea運(yùn)行單個main方法,不編譯整個工程的問題
這篇文章主要介紹了Idea運(yùn)行單個main方法,不編譯整個工程的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04StreamAPI多次消費(fèi)一個stream代碼實例
這篇文章主要介紹了StreamAPI多次消費(fèi)一個stream代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04SpringBoot2.x入門教程之引入jdbc模塊與JdbcTemplate簡單使用方法
這篇文章主要介紹了SpringBoot2.x入門教程之引入jdbc模塊與JdbcTemplate簡單使用方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07springboot整合RabbitMQ 中的 TTL實例代碼
TTL 是 RabbitMQ 中一個消息或者隊列的屬性,表明一條消息或者該隊列中的所有消息的最大存活時間,單位是毫秒,這篇文章主要介紹了springboot整合RabbitMQ 中的 TTL,需要的朋友可以參考下2022-09-09解決Lombok使用@Builder無法build父類屬性的問題
這篇文章主要介紹了解決Lombok使用@Builder無法build父類屬性的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09Java基本數(shù)據(jù)類型(動力節(jié)點java學(xué)院整理)
Java數(shù)據(jù)類型(type)可以分為兩大類:基本類型(primitive types)和引用類型(reference types)。下面是動力節(jié)點給大家整理java基本數(shù)據(jù)類型相關(guān)知識,感興趣的朋友一起學(xué)習(xí)吧2017-03-03