java完成京東秒殺功效分享 京東秒殺軟件。本站提示廣大學習愛好者:(java完成京東秒殺功效分享 京東秒殺軟件)文章只能為提供參考,不一定能成為您想要的結果。以下是java完成京東秒殺功效分享 京東秒殺軟件正文
簡略引見下功效
1.每隔一段時光(好比1分鐘)在京東手機逐日一秒殺頁面提取產物(手機)鏈接。
http://sale.360buy.com/act/8VTHFGr10CjMDyZ.html#01
2.依據提取到得產物鏈接給後台發送數據,以便獲得產物價錢,描寫,扣頭,庫存(能否有貨)等信息。
3.依據獲得的信息,停止斷定。
若相符前提主動挪用閱讀器(條件是chrome參加情況變量,或許改代碼將閱讀器.exe途徑參加代碼,修正法式)翻開產物訂購頁面。
4.其實也就處理了一個成績:不消本身頻仍的刷新網頁了,不消本身檢查;
上岸,提交定單還得閱讀器處理(貌似這幾個功效點比擬龐雜,沒做處置)
法式做的不太完美:
運轉前須要修正幾個處所:
1.情況變量:chrome 參加閱讀器變量,以便挪用。。或許本身修正源代碼用其它方法翻開。
2.運動中每一個產物的價錢信息須要設置。這個比擬欠好,必需修正源代碼。
修正的處所在filter()這個函數裡。
3.別的一個須要修正的處所是
hasStore(String skuidkey)
address="http://price.360buy.com/stocksoa/StockHandler.ashx?callback=getProvinceStockCallback&type=pcastock&skuid="+skuidkey+"&provinceid=1&cityid=2800&areaid=2850";
這個處所的cityid=2800&areaid=...地裡地位信息。這個沒做處置。須要從手機產物頁本身弄出來。
其實也比擬簡略。chrome+F12 ,修正“城市”,區域等信息後,會看到一個get要求發送到後台,這個鏈接外面包括了須要的信息。(http://price.360buy.com/stocksoa/StockHandler.ashx?callback=getProvinceStockCallback&type=pcastock&skuid=64EBD0F20F593D95C72C6EED59B64658&provinceid=1&cityid=2805&areaid=2854)恰當修正。
Util.java
package view.Util;
import java.util.ArrayList;
public class Util {
public static void print(Object o){
System.out.print(o);
}
public static void println(Object o){
if(null==o)
System.out.println();
else
System.out.println(o);
}
public static ArrayList<Integer> toArrayList(int[] ints){
if(ints.length==0)
return null;
ArrayList<Integer> al=new ArrayList<Integer>();
for(int i=0;i<ints.length;i++){
al.add(ints[i]);
}
return al;
}
}
Miaosha360buy.java
package jingdong;
public class Miaosha360buy {
java.util.concurrent.CountDownLatch t= new java.util.concurrent.CountDownLatch(1);
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName() + "開端");
Miaosha360buy ms360=new Miaosha360buy();
new ThreadOne360buy(ms360.t).start();
while(true){
try {
ms360.t.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.sleep(1000*60);//距離1分鐘挪用一次?
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ms360.t=new java.util.concurrent.CountDownLatch(1);
new ThreadOne360buy(ms360.t).start();
System.out.println("New Tread in while..");
}
}
}
Miaosha360buy.java
package jingdong;
public class Miaosha360buy {
java.util.concurrent.CountDownLatch t= new java.util.concurrent.CountDownLatch(1);
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName() + "開端");
Miaosha360buy ms360=new Miaosha360buy();
new ThreadOne360buy(ms360.t).start();
while(true){
try {
ms360.t.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.sleep(1000*60);//距離1分鐘挪用一次?
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ms360.t=new java.util.concurrent.CountDownLatch(1);
new ThreadOne360buy(ms360.t).start();
System.out.println("New Tread in while..");
}
}
}
ThreadOne360buy.java
package jingdong;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import view.Util.Util;
public class ThreadOne360buy extends Thread{
java.util.concurrent.CountDownLatch c;
ArrayList al;//記載秒殺產物頁面
float price=0.0f;//商品價錢
float discount=0.0f;//商品扣頭
//用於保留線程信息,在這個項目裡用途不年夜
private static List<Thread> runningThreads = new ArrayList<Thread>();
//這個是一個計數器(不太會用,線程這方面一向感到是比擬龐雜的)
public ThreadOne360buy(java.util.concurrent.CountDownLatch c) {
this.c=c;
}
@Override
public void run() {
regist(this);// 線程開端時注冊
// 打印開端標志
System.out.println(Thread.currentThread().getName() + "開端...");
try {
//抓取京東手機秒殺頁面
this.getMessage("http://sale.360buy.com/act/8VTHFGr10CjMDyZ.html#01");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
c.countDown();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
c.countDown();
}
c.countDown();
unRegist(this);// 線程停止時撤消注冊
// 打印停止標志
System.out.println(Thread.currentThread().getName() + "停止.");
}
public void regist(Thread t) {
synchronized (runningThreads) {
runningThreads.add(t);
}
}
public void unRegist(Thread t) {
synchronized (runningThreads) {
runningThreads.remove(t);
}
}
public static boolean hasThreadRunning() {
// 經由過程斷定runningThreads能否為空就可以曉得能否還有線程未履行完
return (runningThreads.size() > 0);
}
/**
* 從手機秒殺頁面獲得 prodcut鏈接,product skuid,skuidkey,price,store信息
* @param url:手機秒殺頁面
* @throws ClientProtocolException
* @throws IOException
*/
public void getMessage(String url) throws ClientProtocolException, IOException{
al=getMainUrl(down(url));
Util.println(al);
if(al.size()==0){
c.countDown();
System.exit(0);
return;
}
for(int i=0;i<al.size();i++){
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());
HashMap<String, String> hm=parseProduct(down(sb.toString()));
//用來婚配價錢信息。婚配庫存信息
filter(hm,openUrl.toString());//過濾價錢,假如前提相符就翻開閱讀器
}
}
/**
* 一個驗證辦法
* @param hm 保留著價錢信息
* @param url 產物頁面
*/
public void filter(HashMap<String, String> hm,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);
// }
}
}
/**
* 解析了產物頁面中name,skuid,price信息
* @param doc
* @return
*/
public static HashMap<String, String> parseProduct(Document doc){
String text=doc.text();
String docc=text.substring(text.indexOf("master")+9,text.indexOf("fittings")-3).replaceAll("[\\s]", "");
String[] ss=docc.split(",");
HashMap<String, String> hm=new HashMap<String, String>();
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;
}
/**
* 處置unicode字符,轉換成顯示字符(漢字),不太通用
* @param it: \u6a5d
* @return
*/
public static String unicodeDecode(String it){//有個缺陷,就是後面的字符沒法去失落
Util.println(it);
String regex="(\\\\u[0-9a-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.appendWordStrment(sba,sb.append((char )Integer.parseInt((mc.group(1).substring(2)), 16)).toString());
}
return sba.toString();
}
/**
* 前往文檔對象(下載內容)
* @param url 下載頁面
* @return
* @throws ClientProtocolException
* @throws IOException
*/
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;
}
/**
* 參加了編碼掌握信息
* @param url 待下載頁面
* @param code 編碼
* @return
* @throws ClientProtocolException
* @throws IOException
*/
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;
}
/**
* 用來解析 秒殺頁面中的產物(搜集)鏈接
* @param doc
* @return
*/
public static ArrayList<String> getMainUrl(Document doc){
if(doc.equals("")||doc==null)
return null;
try {
Thread.sleep(50);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ArrayList<String> urls=new ArrayList<String>();
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;
}
/**
* 獲得skuidkey,用於查詢商品庫存信息
* @param url
* @return
*/
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;
}
/**
* 檢查庫存信息
* @param skuidkey
* @return
*/
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;
}
/* if(array[1]=="34"||array[1]=="18"){
changeCart(false);
djdarea.stockInfoDom.html("<strong class='store-over'>無貨</strong>");
}
else if(array[1]=="0"){
changeCart(false);
djdarea.stockInfoDom.html("<strong class='store-over'>無貨</strong>");
}
else if(array[2]=="0"&&array[4]!="2"){
changeCart(false);
djdarea.stockInfoDom.html("很負疚,該商品沒法送至您選擇的區域");
}
else if(array[1]=="33"||array[1]=="5"){
changeCart(true);
djdarea.stockInfoDom.html("<strong>現貨</strong>"+(array[4]=="1"?",該區域"+(array[3]=="0"?"不":"")+"支撐貨到付款":"")+cashdesc);
}
else if(array[1]=="36"){
changeCart(true);
djdarea.stockInfoDom.html("<strong>預訂</strong>"+(array[4]=="1"?",該區域"+(array[3]=="0"?"不":"")+"支撐貨到付款":"")+cashdesc);
}
else if(array[1]=="39"){
changeCart(true);
djdarea.stockInfoDom.html("<strong>在途</strong>"+(array[4]=="1"?",該區域"+(array[3]=="0"?"不":"")+"支撐貨到付款":"")+cashdesc);
}
else if(array[1]=="40"){
changeCart(true);
djdarea.stockInfoDom.html("<strong>可配貨</strong>"+(array[4]=="1"?",該區域"+(array[3]=="0"?"不":"")+"支撐貨到付款":"")+cashdesc);
}
*/
/**
* 解析庫存信息
* @param doc
* @return
*/
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;
}
}
oenCMD.java
package view.Util;
public class oenCMD {
// public static void main(String[] args) {
//// openWinExe(null);
// openExe(null,"http://www.百度.com");
// }
//用 Java 挪用windows體系的exe文件,好比notepad,calc之類
public static void openWinExe(String command,String url) {
if(command==null ||command.equals("")){
command = "chrome "+url;
}
Runtime rn = Runtime.getRuntime();
Process p = null;
try {
p = rn.exec(command);
} catch (Exception e) {
System.out.println("Error win exec!");
}
}
//挪用其他的可履行文件,例如:本身制造的exe,或是 下載 裝置的軟件.
public static void openExe(String pathAndName,String url) {
if(pathAndName==null || pathAndName.equals("")){
pathAndName="C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe";
}
if(url!=null && !url.equals("")){
pathAndName+=" ";
pathAndName+=url;
}
Runtime rn = Runtime.getRuntime();
Process p = null;
try {
p = rn.exec(pathAndName);
} catch (Exception e) {
System.out.println("Error exec!");
}
}
}