@Override public LotteryDraw fetchResultDetail(String phase) { LotteryDraw lotteryDraw = null; lotteryDraw = nowPhaseResult(); if (phase == null || "".equals(phase) || lotteryDraw.getPhase().equals(phase)) { return lotteryDraw; } else { lotteryDraw = null; } String url = RESULT_MORE_LOCALITY_URL; String data = null; String pageInfo = "结果页面" + url; String encoding = "utf-8"; String logHeader = "==" + lotteryScope + "==" + siteName + "==" + pageInfo + "==抓取==" + getLotteryType().getName() + "=="; try { data = CoreFetcherUtils.URLGet(url, null, encoding); } catch (Exception e) { logger.error("获取html数据失败" + e.getMessage()); return null; } if (data == null || data.indexOf("404 Not Found") > 0 || data.isEmpty()) { logger.error(logHeader + "data is null or 404 Not Found"); return null; } Parser parser = null; try { parser = Parser.createParser(data, encoding); } catch (Exception e) { logger.error("解析html页面失败" + e.getMessage()); return null; } NodeFilter filter = new HasAttributeFilter("class", "mytable"); NodeList nodeList = null; try { nodeList = parser.extractAllNodesThatMatch(filter); TableTag tableTag = (TableTag) nodeList.elementAt(0); TableRow[] tableRows = tableTag.getRows(); for (int i = 1; i < tableRows.length; i++) { TableColumn[] tableColumns = tableRows[i].getColumns(); String phaseTmp = tableColumns[0].toPlainTextString(); if (phaseTmp != null && !"".equals(phaseTmp) && phase.equals(phaseTmp)) { lotteryDraw = new LotteryDraw(); // 彩期 lotteryDraw.setPhase(phaseTmp); // 开奖结果 String strResult = tableColumns[1].toPlainTextString(); strResult = strResult.trim().replace(" ", ","); lotteryDraw.setResult(strResult); // 彩种 lotteryDraw.setLotteryType(super.getLotteryType()); break; } } } catch (ParserException e) { logger.error("数据解析错误==" + e.getMessage(), e); return null; } return lotteryDraw; }
private LotteryDraw nowPhaseResult() { String url = RESULT_LOCALITY_URL; LotteryDraw lotteryDraw = new LotteryDraw(); String data = null; String pageInfo = "结果页面" + url; String encoding = "utf-8"; String logHeader = "==" + lotteryScope + "==" + siteName + "==" + pageInfo + "==抓取==" + getLotteryType().getName() + "=="; try { data = CoreFetcherUtils.URLGet(url, null, encoding); } catch (Exception e) { logger.error("获取xml数据失败" + e.getMessage()); return null; } if (data == null || data.indexOf("404 Not Found") > 0 || data.isEmpty()) { logger.error(logHeader + "data is null or 404 Not Found"); return null; } List<LotteryDrawPrizeItem> lotteryDrawPrizeItemList = new ArrayList<LotteryDrawPrizeItem>(); SAXReader saxReader = new SAXReader(); Document document = DocumentFactory.getInstance().createDocument(); try { ByteArrayInputStream bais = new ByteArrayInputStream(data.getBytes(encoding)); document = saxReader.read(bais); Iterator<?> it = document.getRootElement().elementIterator(); while (it.hasNext()) { Element element = (Element) it.next(); if (!element.getName().equals("po7oprize")) { continue; } Iterator<?> childIt = element.elementIterator(); Element childElement = null; String name = ""; String count = ""; String bonus = ""; while (childIt.hasNext()) { childElement = (Element) childIt.next(); if (childElement.getName().equals("code")) { lotteryDraw.setResult(childElement.getTextTrim()); } if (childElement.getName().equals("term")) { lotteryDraw.setPhase(childElement.getTextTrim()); lotteryDraw.setLotteryType(getLotteryType()); } if (childElement.getName().equals("drawOpenDate")) { lotteryDraw.setTimeDraw(childElement.getTextTrim() + " 00:00:00"); } if (childElement.getName().equals("drawSaleCount")) { lotteryDraw.setVolumeOfSales(childElement.getTextTrim().replace(",", "")); } if (childElement.getName().equals("drawPrizePoolCount")) { lotteryDraw.setJackpot(childElement.getTextTrim().replace(",", "")); } if (childElement.getName().equals("name")) { name = childElement.getTextTrim(); } if (childElement.getName().equals("count2")) { count = childElement.getTextTrim().replace(",", ""); } if (childElement.getName().equals("bonus")) { bonus = childElement.getTextTrim().replace(",", ""); } if (name != null && !"".equals(name) && bonus != null && !"".equals(bonus) && count != null && !"".equals(count)) { LotteryDrawPrizeItem lotteryDrawPrizeItem = new LotteryDrawPrizeItem(); lotteryDrawPrizeItem.setName(name); lotteryDrawPrizeItem.setWinningCount(count); lotteryDrawPrizeItem.setPrizeAmount(bonus); lotteryDrawPrizeItemList.add(lotteryDrawPrizeItem); name = ""; bonus = ""; count = ""; } } } lotteryDraw.setResultDetail(lotteryDrawPrizeItemList); } catch (Exception e) { logger.error("数据解析错误==" + e.getMessage(), e); return null; } return lotteryDraw; }