Javaは宝を洗って秒を殺すことを実現します。
説明
この例では、お得な買い占めボタンを監視し、お得にまとめられた時点で自動的にページを弾き出す(URLは自己定義)。
自動的に監視し続ける分の数を指定できます。マルチスレッドを使って更新速度を上げることもできます。
ソース
この例では、お得な買い占めボタンを監視し、お得にまとめられた時点で自動的にページを弾き出す(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 [email protected]
* @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
");
formFlag = true;
}
if (formFlag == true) {
if (s.indexOf("<input name=\'_tb_token_\' type=\'hidden\' value") != -1) {
content.append(s + "\r
");
}
if (s.indexOf("<input type=\"hidden\" name=\"_input_charset\" value") != -1) {
content.append(s + "\r
");
}
if (s.indexOf("<input type=\"hidden\" name=\"itemId\" value") != -1) {
content.append(s + "\r
");
}
if (s.indexOf("<input type=\"hidden\" name=\"id\" value") != -1) {
content.append(s + "\r
");
}
if (s.indexOf("<input type=\"hidden\" name=\"tgType\" value") != -1) {
content.append(s + "\r
");
}
if (s.indexOf("<input type=\"submit\" class=\"buyaction J_BuySubmit\"") != -1) {
content.append(s + "\r
");
}
if (s.indexOf("</form>") != -1) {
content.append(s + "\r
");
}
}
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
");
}
} 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();
}
}
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。