kaijiang.500.com的数据接口延迟比较大,而且相当不稳定,有时候会一个上午开不出来数据,可以配合其他家的接口一起使用,下面是爬去500的数据:
package org.framework.task.lottery;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.framework.constants.LotteryTemp;
import org.framework.core.http.HttpClient;
import org.framework.core.util.DateUtils;
import org.framework.task.xml.XmlReader;
import org.framework.web.entity.ProductQuery;
import org.framework.web.service.LotteryService;
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.util.Date;
import java.util.List;
/**
* https://kaijiang.500.com/index3.shtml
*
* @author: [email protected]
* @date: 2017/12/11 22:28
*/
@Component
public class CaiPiao500Task {
protected Logger logger = Logger.getLogger(CaiPiao500Task.class);
private static final String BASE_URL = "http://kaijiang.500.com";
@Autowired
private HttpClient httpClient;
@Autowired
public LotteryService lotteryService;
@Value("${product}")
private String product;
@Scheduled(cron = "0/10 * * * * ?")
public void lotteryProc() throws Exception {
if ("false".equals(product)) {
return;
}
//http://kaijiang.500.com/static/info/kaijiang/xml/shdsyxw/newlyopen.xml
//Element eleXuan5 = tbody.get(14); //山东11选5
XmlReader eleXuan5 = xmlReader(httpClient.getExchange(BASE_URL + "/static/info/kaijiang/xml/shdsyxw/newlyopen.xml", null), LotteryTemp.SD_11_5);
updatProductQuery(eleXuan5);
//http://kaijiang.500.com/static/info/kaijiang/xml/dlc/newlyopen.xml
//Element jx11xuan5 = tbody.get(13); //江西11选5
XmlReader jx11xuan5 = xmlReader(httpClient.getExchange(BASE_URL + "/static/info/kaijiang/xml/dlc/newlyopen.xml", null), LotteryTemp.JX_11_5);
updatProductQuery(jx11xuan5);
//http://kaijiang.500.com/static/info/kaijiang/xml/gdsyxw/newlyopen.xml
//Element gd11xuan5 = tbody.get(18);//广东11选5
XmlReader gd11xuan5 = xmlReader(httpClient.getExchange(BASE_URL + "/static/info/kaijiang/xml/gdsyxw/newlyopen.xml", null), LotteryTemp.GD_11_5);
updatProductQuery(gd11xuan5);
//http://kaijiang.500.com/static/info/kaijiang/xml/hljsyxw/newlyopen.xml
//Element hlj11xuan5 = tbody.get(7);//黑龙江[好运11选5]
XmlReader hlj11xuan5 = xmlReader(httpClient.getExchange(BASE_URL + "/static/info/kaijiang/xml/hljsyxw/newlyopen.xml", null), LotteryTemp.HLJ_11_5);
updatProductQuery(hlj11xuan5);
//http://kaijiang.500.com/static/info/kaijiang/xml/zjsyxw/newlyopen.xml
//Element zj11xuan5 = tbody.get(10); //浙江 [易乐11选5]
XmlReader zj11xuan5 = xmlReader(httpClient.getExchange(BASE_URL + "/static/info/kaijiang/xml/zjsyxw/newlyopen.xml", null), LotteryTemp.ZJ_11_5);
updatProductQuery(zj11xuan5);
//Element ln11xuan5 = tbody.get(10); //辽宁11选5
XmlReader ln11xuan5 = xmlReader(httpClient.getExchange(BASE_URL + "/static/info/kaijiang/xml/lnsyxw/newlyopen.xml", null), LotteryTemp.LN_11_5);
updatProductQuery(ln11xuan5);
/*Element kuai3 = trs.get(5);*/
//http://kaijiang.500.com/static/info/kaijiang/xml/jsk3/newlyopen.xml
//Element jskuai3 = tbody.get(9); //江苏 [新快3]
XmlReader jskuai3 = xmlReader(httpClient.getExchange(BASE_URL + "/static/info/kaijiang/xml/jsk3/newlyopen.xml", null), LotteryTemp.JS_KUAI_3);
updatProductQuery(jskuai3);
//http://kaijiang.500.com/static/info/kaijiang/xml/gxk3/newlyopen.xml
//Element gxkuai3 = tbody.get(19); //广西快三
XmlReader gxkuai3 = xmlReader(httpClient.getExchange(BASE_URL + "/static/info/kaijiang/xml/gxk3/newlyopen.xml", null), LotteryTemp.GX_KUAI_3);
updatProductQuery(gxkuai3);
//http://kaijiang.500.com/static/info/kaijiang/xml/hbk3/newlyopen.xml
//Element hbkuai3 = tbody.get(16); //湖北
XmlReader hbkuai3 = xmlReader(httpClient.getExchange(BASE_URL + "/static/info/kaijiang/xml/hbk3/newlyopen.xml", null), LotteryTemp.HB_KUAI_3);
updatProductQuery(hbkuai3);
//http://kaijiang.500.com/static/info/kaijiang/xml/nmgk3/newlyopen.xml
//Element nmgkuai3 = tbody.get(4); //内蒙古
XmlReader nmgkuai3 = xmlReader(httpClient.getExchange(BASE_URL + "/static/info/kaijiang/xml/nmgk3/newlyopen.xml", null), LotteryTemp.NMG_KUAI_3);
updatProductQuery(nmgkuai3);
//http://kaijiang.500.com/static/info/kaijiang/xml/ahk3/newlyopen.xml
//Element ahkuai3 = tbody.get(11); //安徽
XmlReader ahkuai3 = xmlReader(httpClient.getExchange(BASE_URL + "/static/info/kaijiang/xml/ahk3/newlyopen.xml", null), LotteryTemp.AH_KUAI_3);
updatProductQuery(ahkuai3);
//http://kaijiang.500.com/static/public/ssc/xml/newlyopen.xml
//Element cqssc = tbody.get(20).getElementsByTag("tr").get(0);//重庆时时彩
XmlReader cqssc = xmlReader(httpClient.getExchange(BASE_URL + "/static/public/ssc/xml/newlyopen.xml", null), LotteryTemp.CQ_SSC);
updatProductQuery(cqssc);
//http://kaijiang.500.com/static/info/kaijiang/xml/ynssc/newlyopen.xml 云南时时彩
XmlReader ynssc = xmlReader(httpClient.getExchange(BASE_URL+"/static/info/kaijiang/xml/ynssc/newlyopen.xml" , null) , LotteryTemp.YN_SSC);
updatProductQuery(ynssc);
//http://kaijiang.500.com/static/info/kaijiang/xml/tjssc/newlyopen.xml 天津时时彩
XmlReader tjssc = xmlReader(httpClient.getExchange(BASE_URL+"/static/info/kaijiang/xml/tjssc/newlyopen.xml" , null) , LotteryTemp.TJ_SSC);
tjssc.setExpect(tjssc.getExpect().substring(2,11));
updatProductQuery(tjssc);
//http://kaijiang.500.com/static/info/kaijiang/xml/xjssc/newlyopen.xml 新疆时时彩
XmlReader xjssc = xmlReader(httpClient.getExchange(BASE_URL+"/static/info/kaijiang/xml/xjssc/newlyopen.xml" , null) , LotteryTemp.XJ_SSC);
xjssc.setExpect(DateUtils.getYear()+xjssc.getExpect());
xjssc.setExpect(xjssc.getExpect().substring(2,11));
xjssc.setExpect(xjssc.getExpect().substring(0,6)+xjssc.getExpect().substring(7,9));
updatProductQuery(xjssc);
//http://kaijiang.500.com/static/info/kaijiang/xml/bjpkshi/newlyopen.xml 北京PK10
XmlReader bjpk10 = xmlReader(httpClient.getExchange(BASE_URL+"/static/info/kaijiang/xml/bjpkshi/newlyopen.xml" , null) , LotteryTemp.BJ_PK10);
updatProductQuery(bjpk10);
}
public void updatProductQuery(XmlReader xmlReader) {
ProductQuery pq = new ProductQuery();
pq.setUts(new Date());
pq.setSpell(xmlReader.getSpell());
pq.setLotteryNo(xmlReader.getExpect());
//500彩票
pq.setFrom(3);
pq.setLotteryNums(xmlReader.getOpencode());
try {
lotteryService.updateProductQuery(pq);
} catch (Exception e) {
e.printStackTrace();
}
}
private XmlReader xmlReader(String text, String spell) throws DocumentException {
XmlReader xmlReader = new XmlReader();
Document document = DocumentHelper.parseText(text);
Element row = document.getRootElement().element("row");
String expect = row.attributeValue("expect");
switch (spell) {
case LotteryTemp.CQ_SSC:
expect = expect.substring(2, expect.length());
break;
case LotteryTemp.GX_KUAI_3:
expect = DateUtils.formatDate().substring(0, 2) + expect;
break;
case LotteryTemp.LN_11_5:
expect = expect.replaceAll("-", "");
break;
case LotteryTemp.HLJ_11_5:
expect = expect.replaceAll("-", "");
break;
case LotteryTemp.ZJ_11_5:
expect = expect.replaceAll("-", "");
break;
case LotteryTemp.JX_11_5:
expect = expect.replaceAll("-", "");
break;
case LotteryTemp.SD_11_5:
expect = expect.replaceAll("-", "");
break;
case LotteryTemp.GD_11_5:
expect = expect.replaceAll("-", "");
break;
default:
break;
}
xmlReader.setExpect(expect);
xmlReader.setOpencode(arrayToStr(row.attributeValue("opencode").split(",")));
xmlReader.setSpell(spell);
return xmlReader;
}
private String arrayToStr(String[] array) {
StringBuffer sbf = new StringBuffer();
for (String str : array) {
sbf.append(str);
}
return sbf.toString();
}
private void getXml(String date) throws DocumentException {
String url = "http://kaijiang.500.com/static/info/kaijiang/xml/zjsyxw/" + date + ".xml";
String spell = LotteryTemp.ZJ_11_5;
String text = httpClient.getExchange(url, null);
try {
Document document = DocumentHelper.parseText(text);
Element element = document.getRootElement();
logger.info(element);
List elements = element.elements();
logger.info(elements.size());
XmlReader xmlReader = null;
for (Element element1 : elements) {
xmlReader = new XmlReader();
String expect = element1.attributeValue("expect");
switch (spell) {
case LotteryTemp.CQ_SSC:
expect = expect.substring(2, expect.length());
break;
case LotteryTemp.GX_KUAI_3:
expect = DateUtils.formatDate().substring(0, 2) + expect;
break;
case LotteryTemp.LN_11_5:
expect = expect.replaceAll("-", "");
break;
case LotteryTemp.HLJ_11_5:
expect = expect.replaceAll("-", "");
break;
case LotteryTemp.ZJ_11_5:
expect = expect.replaceAll("-", "");
break;
default:
break;
}
xmlReader.setExpect(expect);
xmlReader.setOpencode(arrayToStr(element1.attributeValue("opencode").split(",")));
this.updatProductQuery(xmlReader);
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
}