這篇文章給大家分享的是有關(guān)Java怎么實(shí)現(xiàn)爬取往期所有雙色球開(kāi)獎(jiǎng)結(jié)果功能的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
創(chuàng)新互聯(lián)建站網(wǎng)絡(luò)公司擁有10年的成都網(wǎng)站開(kāi)發(fā)建設(shè)經(jīng)驗(yàn),上1000+客戶的共同信賴(lài)。提供成都做網(wǎng)站、成都網(wǎng)站建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站開(kāi)發(fā)、網(wǎng)站定制、賣(mài)友情鏈接、建網(wǎng)站、網(wǎng)站搭建、響應(yīng)式網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)師打造企業(yè)風(fēng)格,提供周到的售前咨詢(xún)和貼心的售后服務(wù)
具體如下:
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 + "頁(yè)丟失"); } 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é)果頁(yè)數(shù)為0"); } System.out.println("完成!"); } /** * 獲取總頁(yè)數(shù) * @param result */ private static int getPageCount(String result) { String regex = "\\d+\">末頁(yè)"; 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)頁(yè)源碼 * @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"); //為什么沒(méi)有deflate呢 connection.setRequestProperty("Accept-Encoding", "gzip"); connection.setRequestProperty("Content-type", "text/html"); //keep-Alive,有什么用呢,你不是在訪問(wèn)網(wǎng)站,你是在采集。嘿嘿。減輕別人的壓力,也是減輕自己。 connection.setRequestProperty("Connection", "close"); //不要用cache,用了也沒(méi)有什么用,因?yàn)槲覀儾粫?huì)經(jīng)常對(duì)一個(gè)鏈接頻繁訪問(wèn)。(針對(duì)程序) 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怎么實(shí)現(xiàn)爬取往期所有雙色球開(kāi)獎(jiǎng)結(jié)果功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
文章標(biāo)題:Java怎么實(shí)現(xiàn)爬取往期所有雙色球開(kāi)獎(jiǎng)結(jié)果功能
標(biāo)題URL:http://chinadenli.net/article6/jieiog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開(kāi)發(fā)、手機(jī)網(wǎng)站建設(shè)、用戶體驗(yàn)、網(wǎng)站排名、網(wǎng)站改版、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)