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

 更新时间:2020年09月29日 14:19:06   作者:Techzero  
这篇文章主要为大家详细介绍了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; 
 
/** 
 * PriceMonitor.java 
 * 
 * @author Techzero 
 * @Email techzero@163.com 
 * @Time 2014-5-21 下午1:24:30 
 */ 
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("<input type=\"submit\" class=\"buyaction J_BuySubmit\" title=\"马上抢\" value=\"马上抢\"/>") != -1) { 
     buttonState = "马上抢"; 
    } else if (s.indexOf("<a href=\"#\" class=\"extra notice J_BuyButtonSub\">开团提醒</a>") != -1) { 
     buttonState = "开团提醒"; 
    } else if (s.indexOf("<div class=\"main-box chance \">") != -1) { 
     buttonState = "还有机会"; 
    } else if (s.indexOf("<span class=\"out floatright\">卖光了...</span>") != -1) { 
     buttonState = "卖光了"; 
    } else if (s.indexOf("<span class=\"out floatright\">已结束...</span>") != -1) { 
     buttonState = "已结束"; 
    } 
    if (s.indexOf("<form class=\"J_BuySubForm\" data-ccb=\"0\" data-ques=\"0\" action") != -1) { 
     content.append(s + "\r\n"); 
     formFlag = true; 
    } 
    if (formFlag == true) { 
     if (s.indexOf("<input name=\'_tb_token_\' type=\'hidden\' value") != -1) { 
      content.append(s + "\r\n"); 
     } 
     if (s.indexOf("<input type=\"hidden\" name=\"_input_charset\" value") != -1) { 
      content.append(s + "\r\n"); 
     } 
     if (s.indexOf("<input type=\"hidden\" name=\"itemId\" value") != -1) { 
      content.append(s + "\r\n"); 
     } 
     if (s.indexOf("<input type=\"hidden\" name=\"id\" value") != -1) { 
      content.append(s + "\r\n"); 
     } 
     if (s.indexOf("<input type=\"hidden\" name=\"tgType\" value") != -1) { 
      content.append(s + "\r\n"); 
     } 
     if (s.indexOf("<input type=\"submit\" class=\"buyaction J_BuySubmit\"") != -1) { 
      content.append(s + "\r\n"); 
     } 
     if (s.indexOf("</form>") != -1) { 
      content.append(s + "\r\n"); 
     } 
    } 
    if (s.indexOf("<div class=\"time-banner\">") != -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 + "\r\n"); 
   } 
  } catch (Exception e) { 
   e.printStackTrace(); 
  } 
  // 返回提交表单后返回的页面内容 
  return content.toString(); 
 } 
  
 // 登录 
 public static void doLogin(String username, String password) { 
  String form = "<form id=\"J_StaticForm\" action=\"https://login.taobao.com/member/login.jhtml\" method=\"post\" autocomplete=\"on\"><input type=\"text\" name=\"TPL_username\" id=\"TPL_username_1\" value=\"" + username + "\"><input type=\"password\" name=\"TPL_password\" id=\"TPL_password_1\" value=\"" + password + "\"><input type=\"hidden\" id=\"J_TPL_redirect_url\" name=\"TPL_redirect_url\" value=\"http://www.taobao.com/?spm=a2107.1.1000340.1.AL2Mpn\"><button type=\"submit\" id=\"J_SubmitStatic\">登 录</button></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(); 
 } 
} 

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

相关文章

  • 关于Spring Validation数据校检的使用流程分析

    关于Spring Validation数据校检的使用流程分析

    在实际项目中,对客户端传递到服务端的参数进行校验至关重要,SpringValidation提供了一种便捷的方式来实现这一需求,通过在POJO类的属性上添加检查注解,本文给大家介绍Spring Validation数据校检的使用流程,感兴趣的朋友一起看看吧
    2024-11-11
  • 详解spring Boot Cli的配置和使用

    详解spring Boot Cli的配置和使用

    本篇文章主要介绍了详解spring Boot Cli的配置和使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • 一文彻底搞懂Java日期时间类详解

    一文彻底搞懂Java日期时间类详解

    这篇文章主要给大家介绍了关于Java日期时间类的相关资料,Calendar类的功能要比Date类强大很多,可以方便的进行日期的计算,获取日期中的信息时考虑了时区等问题,需要的朋友可以参考下
    2023-10-10
  • Feign Client超时时间设置不生效的解决方法

    Feign Client超时时间设置不生效的解决方法

    这篇文章主要为大家详细介绍了Feign Client 超时时间设置不生效的原因与解决方法,具有一定的的参考价值,希望对大家有一定的帮助
    2025-04-04
  • Java中outer标签的用法实例代码

    Java中outer标签的用法实例代码

    这篇文章主要介绍了Java中outer标签的用法,在这里需要大家注意这里的outer并不是关键字,而仅仅是一个标签,本文结合实例代码给大家详细讲解,需要的朋友可以参考下
    2023-01-01
  • Java处理Markdown格式转换为Word文档

    Java处理Markdown格式转换为Word文档

    这篇文章主要为大家详细介绍了如何使用Java实现处理Markdown格式转换为Word文档,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下
    2025-03-03
  • Maven配置单仓库与多仓库的实现(Nexus)

    Maven配置单仓库与多仓库的实现(Nexus)

    本文主要介绍了Maven配置单仓库与多仓库的实现(Nexus),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-01-01
  • Java常用字符串工具类 字符串智能截取(3)

    Java常用字符串工具类 字符串智能截取(3)

    这篇文章主要为大家详细介绍了Java常用字符串工具类,字符串的智能截取,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • 基于SpringBoot实现Web应用的登录与退出功能

    基于SpringBoot实现Web应用的登录与退出功能

    登录与退出功能作为 Web 应用中的基础且重要的组成部分,直接关系到用户的安全和隐私保护,所以本文给大家介绍了基于SpringBoot实现Web应用的登录与退出功能,文中有详细的代码供大家参考,需要的朋友可以参考下
    2024-04-04
  • struts2通过action返回json对象

    struts2通过action返回json对象

    struts2通过action返回json对象其实很简单的,首先我们需要引入jar包,然后在写一个简单的action就好了,接下来通过本文给大家介绍struts2通过action返回json对象的方法,感兴趣的朋友一起看看吧
    2016-09-09

最新评论