栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java实现淘宝秒杀聚划算抢购自动提醒源码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java实现淘宝秒杀聚划算抢购自动提醒源码

说明

本实例能够监控聚划算的抢购按钮,在聚划算整点聚的时间到达时自动弹开页面(URL自己定义)。

可以自定义监控持续分钟数,同时还可以通过多线程加快刷新速度。

源码

package com.itechzero.pricemonitor; 
 
import java.io.BufferedInputStream; 
import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.URI; 
import java.net.URL; 
import java.net.URLConnection; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
 
 
class MyThread extends Thread { 
 public void run() { 
  try { 
   // 此处参数为监控持续分钟数 
   PriceMonitor.monitorButton(10); 
  } catch (Exception e) { 
   e.printStackTrace(); 
  } 
 } 
}; 
 
public class PriceMonitor { 
 // 监控的商品URL 
 private static String URL = "http://detail.ju.taobao.com/home.htm?spm=608.2214381.3.1.AdPEjn&item_id=38260927591&id=10000002781939"; 
 
 // 监视按钮 
 public static void monitorButton(int lastMinute) { 
  int nowMinute = Integer.parseInt(new SimpleDateFormat("mm").format(new Date())); 
  int endMinute = Integer.parseInt(new SimpleDateFormat("mm").format(new Date())) + lastMinute; 
  while (nowMinute < endMinute) { 
   nowMinute = Integer.parseInt(new SimpleDateFormat("mm").format(new Date())); 
   String result[] = getCurrentButtonAndForm(URL, "gb2312").split(","); 
   // 当前按钮状态 
   String currentButton = result[0]; 
   // 马上抢 表单 
   //String form = result[1]; 
   String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); 
   System.out.println(nowTime + " - 现在按钮是 " + currentButton); 
 
   if (currentButton == "马上抢" || currentButton.equals("马上抢") || currentButton == "还有机会" || currentButton.equals("还有机会")) { 
    System.out.println("赶紧下单!"); 
    try { 
     java.awt.Desktop.getDesktop().browse(new URI(URL)); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    //doPost(form); 
    break; 
   } else if (currentButton == "卖光了" || currentButton.equals("卖光了") || currentButton.equals("已结束") || currentButton.equals("已结束")) { 
    System.out.println("下次再试吧!"); 
    break; 
   } else { 
    System.out.println("还没开始呢,再等等吧!"); 
   } 
  } 
 } 
 
 // 获取当前按钮状态 
 public static String getCurrentButtonAndForm(String url, String encoding) { 
  if (url == null || "".equals(url.trim())) 
   return null; 
  String buttonState = ""; 
  StringBuffer content = new StringBuffer(); 
  boolean formFlag = false; 
  try { 
   // 新建URL对象 
   URL u = new URL(url); 
   InputStream is = new BufferedInputStream(u.openStream()); 
   InputStreamReader theHTML = new InputStreamReader(is, encoding != null ? encoding : "gb2312"); 
   BufferedReader br = new BufferedReader(theHTML); 
   String s = ""; 
   while ((s = br.readLine()) != null) { 
    if (s.indexOf("") != -1) { 
     buttonState = "马上抢"; 
    } else if (s.indexOf("开团提醒") != -1) { 
     buttonState = "开团提醒"; 
    } else if (s.indexOf("") != -1) { 
     buttonState = "还有机会"; 
    } else if (s.indexOf("卖光了...") != -1) { 
     buttonState = "卖光了"; 
    } else if (s.indexOf("已结束...") != -1) { 
     buttonState = "已结束"; 
    } 
    if (s.indexOf("") != -1) { 
      content.append(s + "rn"); 
     } 
    } 
    if (s.indexOf("") != -1) { 
     break; 
    } 
   } 
   br.close(); 
  } catch (Exception e) { 
   System.err.println(e); 
   return "Open URL Error"; 
  } 
  return buttonState + "," + content; 
 } 
 
 // 提交表单 
 public static String doPost(String form) { 
  StringBuffer content = new StringBuffer(); 
  try { 
   URLConnection connection = new URL(URL).openConnection(); 
   connection.setDoOutput(true); 
   OutputStreamWriter os = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); 
   os.write(form); 
   os.flush(); 
   os.close(); 
   InputStream is = connection.getInputStream(); 
   InputStreamReader theHTML = new InputStreamReader(is); 
   BufferedReader br = new BufferedReader(theHTML); 
   String s = ""; 
   while ((s = br.readLine()) != null) { 
    content.append(s + "rn"); 
   } 
  } catch (Exception e) { 
   e.printStackTrace(); 
  } 
  // 返回提交表单后返回的页面内容 
  return content.toString(); 
 } 
  
 // 登录 
 public static void doLogin(String username, String password) { 
  String form = ""; 
  doPost(form); 
 } 
 
 public static void main(String[] args) { 
  //doLogin(); 
  // new MyThread().start(); 
  // new MyThread().start(); 
  // new MyThread().start(); 
  // new MyThread().start(); 
  // new MyThread().start(); 
  // new MyThread().start(); 
  // new MyThread().start(); 
  new MyThread().start(); 
 } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/141685.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号