Java实现淘宝秒杀聚划算抢购自动提醒源码
作者:Techzero 时间:2022-09-11 10:11:53
说明
本实例能够监控聚划算的抢购按钮,在聚划算整点聚的时间到达时自动弹开页面(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();
}
}
来源:http://blog.csdn.net/Techzero/article/details/26686283
标签:java,淘宝秒杀,抢购
0
投稿
猜你喜欢
C#直线的最小二乘法线性回归运算实例
2022-05-03 11:19:33
java多线程编程学习(线程间通信)
2023-04-02 05:25:34
使用Springboot搭建OAuth2.0 Server的方法示例
2023-01-28 07:19:16
Android实现京东App分类页面效果
2023-05-07 02:53:49
Java抽象类与接口区别详解
2021-06-19 19:22:37
android实现在横竖屏切换时页面信息不被重置的示例分享
2021-06-20 08:45:46
Android使用DrawerLayout实现仿QQ双向侧滑菜单
2023-07-18 23:48:24
Android简单实现一个颜色渐变的ProgressBar的方法
2023-07-23 00:30:12
Java struts2请求源码分析案例详解
2021-08-06 02:03:24
利用thrift实现js与C#通讯的实例代码
2022-09-07 15:49:13
关于Java实现HttpServer模拟前端接口调用
2021-07-10 07:44:17
Java NIO Path接口和Files类配合操作文件的实例
2023-10-20 09:29:01
C#文件和字节流的转换方法
2022-03-18 05:39:43
IDEA导入Eclipse项目的方法步骤(图文教程)
2023-07-17 09:45:09
完全解析Android多线程中线程池ThreadPool的原理和使用
2022-04-07 20:19:40
C# FileStream简单介绍和使用
2023-07-05 23:04:05
Android 处理OnItemClickListener时关于焦点颜色的设置问题
2021-09-02 01:47:00
Springboot 项目读取Resources目录下的文件(推荐)
2023-07-27 09:40:47
AlertDialog点击按钮不消失的实现方法
2023-12-12 07:11:16
教你使用Java获取当前时间戳的详细代码
2021-09-19 04:41:02