在2015年互联网还没有禁售彩票之前,国内各大互联网巨头纷纷都在做彩票业务,包括网易、搜狐、百度、京东、淘宝、腾讯、360等等,可想而知,在互联网公司技术成型之后,都希望涉足这方面的业务,可是2015年国家颁发禁售令之后,所有线上的购彩通道全部关闭,作为一个技术出身的码农,觉得这里面数据挺有意思,好在各家的开彩数据都没停,同时也能洞悉各家的技术规范和风格。下面就以网易数据作为参考(个人觉得网易的产品设计做的是最好的):
package org.framework.task.lottery;
import com.alibaba.fastjson.JSON;
import org.apache.log4j.Logger;
import org.framework.constants.LotteryTemp;
import org.framework.web.entity.ProductQuery;
import org.framework.web.service.LotteryService;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Date;
/**
* @author: [email protected]
* @date: 2017/12/11 22:06
*/
@Component
public class WangYiTask {
private Logger logger = Logger.getLogger(WangYiTask.class);
private static final String BASE_URL = "https://caipiao.163.com/award";
@Autowired
public LotteryService lotteryService;
@Scheduled(cron = "0/10 * * * * ?")
public void runCraw() throws IOException {
Connection conn = Jsoup.connect(BASE_URL);
Document doc = conn.get();
// 页面中有三类彩票, 分别包含在三个table中,table的类名都是<table class="awardList">
// 每类彩票table的<tbody>选出来, 每行一个彩种
Elements els = doc.select(".awardList");
// 数字彩票
Element welfareBody = els.get(0).select("tbody").get(0);
// 竞技体育
Element sportsBody = els.get(1).select("tbody").get(0);
// 高频彩
Element freLotteryBody = els.get(2).select("tbody").get(0);
this.lotteryProduct(freLotteryBody);
this.initWelfare(welfareBody);
this.initSports(sportsBody);
}
/**
* 包括福彩和体彩
*
* @param element
*/
public void initWelfare(Element element) {
Elements trs = element.select("tr");
//双色球
Element ssq = trs.get(0);
this.saveLottery(ssq, LotteryTemp.SCQ);
//大乐透
Element dlt = trs.get(1);
this.saveLottery(dlt, LotteryTemp.DLT);
//3D
Element thirdD = trs.get(2);
this.saveLottery(thirdD, LotteryTemp.THIRD_D);
//排列3
Element pl3 = trs.get(3);
this.saveLottery(pl3, LotteryTemp.PL_3);
//排列5
Element pl5 = trs.get(4);
this.saveLottery(pl5, LotteryTemp.PL_5);
//七星彩
Element qxc = trs.get(5);
this.saveLottery(qxc, LotteryTemp.QXC);
//七乐彩
Element qlc = trs.get(6);
this.saveLottery(qlc, LotteryTemp.QLC);
}
/**
* 体育彩票数据比较乱,这一次没有写
* @param element
*/
public void initSports(Element element) {
Elements trs = element.select("tr");
logger.debug(trs.size());
}
/**
* 高频彩,数据比较有规律
* @param element
*/
public void lotteryProduct(Element element) {
Elements trs = element.select("tr");
//山东11选5
Element eleXuan5 = trs.get(0);
saveLotteryProc(eleXuan5, LotteryTemp.SD_11_5);
//江西11选5
Element jx11xuan5 = trs.get(1);
saveLotteryProc(jx11xuan5, LotteryTemp.JX_11_5);
//广东11选5
Element gd11xuan5 = trs.get(2);
saveLotteryProc(gd11xuan5, LotteryTemp.GD_11_5);
//黑龙江[好运11选5]
Element hlj11xuan5 = trs.get(3);
saveLotteryProc(hlj11xuan5, LotteryTemp.HLJ_11_5);
//浙江 [易乐11选5]
Element zj11xuan5 = trs.get(4);
saveLotteryProc(zj11xuan5, LotteryTemp.ZJ_11_5);
//kuai3
Element kuai3 = trs.get(5);
saveLotteryProc(kuai3, LotteryTemp.KUAI_3);
//江苏 [新快3]
Element jskuai3 = trs.get(6);
saveLotteryProc(jskuai3, LotteryTemp.JS_KUAI_3);
//广西快三
Element gxkuai3 = trs.get(7);
saveLotteryProc(gxkuai3, LotteryTemp.GX_KUAI_3);
//湖北
Element hbkuai3 = trs.get(8);
saveLotteryProc(hbkuai3, LotteryTemp.HB_KUAI_3);
//内蒙古
Element nmgkuai3 = trs.get(9);
String nmgNum = nmgkuai3.getElementsByTag("td").get(3).text();
saveLotteryProc(nmgkuai3, LotteryTemp.NMG_KUAI_3);
//安徽
Element ahkuai3 = trs.get(10);
//saveLotteryProc(ahkuai3, LotteryTemp.AH_KUAI_3);
//重庆时时彩
Element cqssc = trs.get(11);
saveLotteryProc(cqssc, LotteryTemp.CQ_SSC);
/*Element jxssc = trs.get(13);
saveLotteryProc(cqssc , "jxssc" );*/
}
/**
* 高频彩票
*
* @param element
* @param spell
*/
public void saveLotteryProc(Element element, String spell) {
ProductQuery productQuery = new ProductQuery();
productQuery.setUts(new Date());
productQuery.setSpell(spell);
productQuery.setLotteryNo(element.getElementsByTag("td").get(1).text().substring(0, element.getElementsByTag("td").get(1).text().length() - 1));
productQuery.setFrom(1);
productQuery.setLotteryNums(element.getElementsByTag("td").get(3).getElementsByTag("em").select(".smallRedball").text().replace(" ", ""));
try {
logger.debug(JSON.toJSONString(productQuery));
lotteryService.updateProductQuery(productQuery);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 福利彩票
*
* @param element
* @param spell
*/
public void saveLottery(Element element, String spell) {
ProductQuery pq = new ProductQuery();
pq.setCts(new Date());
pq.setSpell(spell);
pq.setLotteryNo(element.getElementsByTag("td").get(1).text().substring(0, element.getElementsByTag("td").get(1).text().length() - 1));
pq.setFrom(1);
pq.setLotteryNums(element.getElementsByTag("td").get(3).getElementsByTag("em").text().replace(" ", ""));
try {
lotteryService.updateProductQuery(pq);
} catch (Exception e) {
e.printStackTrace();
}
}
}