概述
StringBuffer sb=new StringBuffer();
StringBuffer openUrl=new StringBuffer();
openUrl.append("http://www.360buy.com/product/");
openUrl.append(al.get(i).toString().subSequence(al.get(i).toString().lastIndexOf('/')+1, al.get(i).toString().lastIndexOf('.')));
openUrl.append(".html");
// 557673
sb.append("http://d.360buy.com/fittingInfo/get?skuId=");
sb.append(al.get(i).toString().subSequence(al.get(i).toString().lastIndexOf('/')+1, al.get(i).toString().lastIndexOf('.')));
sb.append("&callback=Recommend.cbRecoFittings");
Util.println(sb.toString());
//map中保存的是产品name,price,折扣信息
Util.println("Al("+i+") down:"+sb.toString());
HashMaphm=parseProduct(down(sb.toString()));
//用来匹配价格信息。匹配库存信息
filter(hm,openUrl.toString());//过滤价格,如果条件符合就打开浏览器
}
}
public void filter(HashMaphm,String url){//url既是产品页面
// view.Util.oenCMD.openWinExe(null,url);
// 是不是应该先查看库存?
String skuidkey=parseSkuidkey(url);
if(!hasStore(skuidkey)){
Util.println("-------------------------------------");
Util.println("没有库存了!");
Util.println("-------------------------------------");
//减掉计数,以便主线程判断
c.countDown();
//应该结束子线程哦?
return;
}
if(hm.get("skuid").equals("201602")){//判断//摩托罗拉skuid=201602
//这里的价格是写死了,运行前得改过来才行。
this.setPrice(499.0f);
//是不是应该打开控制台?
if(Float.parseFloat(hm.get("price"))<=this.getPrice()){
view.Util.oenCMD.openWinExe(null,url);
}
}else if(hm.get("skuid").equals("675647")){//天语skuid=675647
// //这里的价格是写死了,运行前得改过来才行。
// this.setPrice(699.0f);
// //是不是应该打开控制台?
// if(Float.parseFloat(hm.get("price"))<=this.getPrice()){
// view.Util.oenCMD.openWinExe(null,url);
// }
}
}
public static HashMapparseProduct(document doc){
String text=doc.text();
String docc=text.substring(text.indexOf("master")+9,text.indexOf("fittings")-3).replaceAll("[\s]", "");
String[] ss=docc.split(",");
HashMaphm=new HashMap();
for(String it: ss){
String string=it.replaceAll(""", "");
if(string.contains("\u"))
string=unicodeDecode(string);
String[] str=string.split(":");
hm.put(str[0], str[1]);
}
Util.println(hm);
return hm;
}
public static String unicodeDecode(String it){//有个缺点,就是前面的字符无法去掉
Util.println(it);
String regex="(\a-f]{4})";
Pattern pt=Pattern.compile(regex);
Matcher mc;
StringBuffer sb;
StringBuffer sba=new StringBuffer();
mc=pt.matcher(it);
while(mc.find()){
sb=new StringBuffer();
mc.appendReplacement(sba,sb.append((char )Integer.parseInt((mc.group(1).substring(2)), 16)).toString());
}
return sba.toString();
}
public static document down(String url) throws ClientProtocolException, IOException{
document doc=null;
DefaultHttpClient httpClient=new DefaultHttpClient();
Util.println("DownLoad:"+url);
HttpGet get=new HttpGet(url);
HttpResponse response;
response=httpClient.execute(get);
HttpEntity entity=response.getEntity();
doc=Jsoup.parse(entity.getContent(), "utf-8","");
//释放资源
EntityUtils.consume(entity);
//关闭连接
httpClient.getConnectionManager().shutdown();
return doc;
}
public static document down(String url,String code) throws ClientProtocolException, IOException{
document doc=null;
DefaultHttpClient httpClient=new DefaultHttpClient();
Util.println("DownLoad:"+url);
HttpGet get=new HttpGet(url);
HttpResponse response;
response=httpClient.execute(get);
HttpEntity entity=response.getEntity();
doc=Jsoup.parse(entity.getContent(), code,"");
//释放资源
EntityUtils.consume(entity);
//关闭连接
httpClient.getConnectionManager().shutdown();
return doc;
}
public static ArrayListgetMainUrl(document doc){
if(doc.equals("")||doc==null)
return null;
try {
Thread.sleep(50);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ArrayListurls=new ArrayList();
String rule="map[name=Map] >area[href~=product]";
Elements elements=doc.select(rule);
for (Element e : elements) {
// Util.println(e.absUrl("abs:href"));
urls.add(e.absUrl("abs:href"));
}
return urls;
}
public static String parseSkuidkey(String url){
document doc=null;
try {
doc=down(url,"gb2312");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Util.println(doc.select("script"));
String text=null;
for(Element e : doc.select("script")){
if(e.data().contains("skuidkey:")){
text=e.data();
break;
}
}
//skuidkey:'7D45919EA8242511DAA5CC7C6D7B351C'
text=text.substring(text.indexOf("skuidkey:")+10, text.indexOf("skuidkey:")+42);
Util.println("---------------------------------");
Util.println(text);
return text;
}
public static boolean hasStore(String skuidkey){//这个地方没有处理,直接提取浏览器中的信息
String address=null;
boolean hasStore=false;
if(skuidkey!=null && !"".equals(skuidkey))
address="http://price.360buy.com/stocksoa/StockHandler.ashx?callback=getProvinceStockCallback&type=pcastock&skuid="+skuidkey+"&provinceid=1&cityid=2800&areaid=2850";
else{
Util.println("解析skuidkey错误");
}
try {
if(parseStore(down(address))){
hasStore=true;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return hasStore;
}
public static boolean parseStore(document doc){
String text=doc.text();
String docc=text.substring(text.indexOf("-")-1,text.lastIndexOf(",")-1);
Util.println(docc);
String[] store=docc.split("-");
if(store[1].equals("34") || store[1].equals("18")){
//无货
Util.println("此地无货");
return false;
}else if(store[1].equals("33") || store[1].equals("5")){
//现货
Util.println("此地现货");
return true;
}
Util.println(store[1]);
return false;
}
//几个bean方法
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price=price;
}
public float getDiscount() {
return discount;
}
public void setDiscount(float discount) {
this.discount=discount;
}
}
(编辑:6999la)部分专业内容仅供参考
最后
以上就是活泼白开水为你收集整理的Java秒杀价格,java实现京东秒杀功能分享 京东秒杀软件的全部内容,希望文章能够帮你解决Java秒杀价格,java实现京东秒杀功能分享 京东秒杀软件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复