Java寫的自動搶購紅米note手機(2016/4/6 米粉節)
阿新 • • 發佈:2019-01-28
受同學所託,幫其搶紅米手機,老是搶不到,於是就想著寫個程式,多執行緒實時監聽,雖然最終還是沒搶到,但還是將程式分享給大家。(其實在排除學校網速的影響下,我真懷疑雷總到底有沒有釋出小米手機讓大家搶,所以還是建議大家儘量不要搶小米手機,太坑!)
程式使用java寫的,採用Maven構建的一個專案,採用selenium包。這裡先給出平臺的搭建,後續給出相應的程式程式碼。
1:使用Maven模板構建一個專案
切換到工作目C:\workshop下,輸入
mvn archetype:generate -DgroupId=com.yiibai -DartifactId=NumberGenerator -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
2:修改pom.xml
修改後的內容如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.yiibai</groupId> <artifactId>NumberGenerator</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>NumberGenerator</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.53.0</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-htmlunit-driver</artifactId> <version>2.20</version> </dependency> </dependencies> </project>
3:cd進入project目錄中:C:\\Workshop\\ NumberGenerator中輸入mvn cleaninstall,然後輸入mvneclipse:eclipse
這樣在eclipse中# File -> Import... # General -> ExistingProjects into Workspace # Click next # 將現有工程匯入eclipse中
4: 程式碼
以下是搶購紅米note3的主頁:
以下是程式的程式碼:
MyThread.java
package com.yiibai; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class myThread extends Thread{ private String name; public myThread(String name){ this.name = name; } public void run(){ // 小米官網搶購紅米note3頁面 String baseurl = "http://item.mi.com/buyphone/note3"; // 小米官網登入資訊 String baseurl2 = "https://account.xiaomi.com/pass/serviceLogin?callback=http%3A%2F%2Forder.mi.com%2Flogin%2Fcallback%3Ffollowup%3Dhttp%253A%252F%252Fitem.mi.com%252Fbuyphone%252Fnote3%26sign%3DZmYyZmU3NmE0NWNmMjZkYzk5YmU5ZWJjMzc2Zjg1YzNhMjY3NTJjMg%2C%2C&sid=mi_eshop"; WebElement loginElement = null; List<WebElement> element = null; List<WebElement> taocan = null; WebElement btn = null; WebDriver driver = new FirefoxDriver(); System.out.println("starting:"); driver.get(baseurl2); driver.findElement(By.id("username")).sendKeys("13721035842"); // 輸入自己小米官網登入賬號 driver.findElement(By.id("pwd")).sendKeys("19910715aabb"); // 輸入小米官網登入密碼 driver.findElement(By.id("login-button")).click(); System.out.println(driver.getCurrentUrl()); try { sleep(5000); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } driver.navigate().to(baseurl); System.out.println("hello world!!"); element = driver.findElements(By.className("J_stepItem")); if(element == null || element.size() == 0){ driver.close(); } element.get(1).click(); // 選擇高配版 3GB記憶體 + 32GB容量 element = driver.findElements(By.className("J_stepItem")); element.get(2).click(); // 選擇雙網通 element = driver.findElements(By.className("J_stepItem")); element.get(5).click(); // 選擇銀白色 taocan = driver.findElements(By.className("J_packageItem")); // 看是否到時間出現套餐選擇 while(taocan == null || taocan.size() == 0){ taocan = driver.findElements(By.className("J_packageItem")); // 一直迴圈等待套餐的出現 System.out.println("listening11...." + name); try { sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } taocan.get(0).click(); // 等到選擇第一個套餐 element = driver.findElements(By.className("btn")); element.get(0).click(); // 點選下一步搶購按鈕 等待 //driver.quit(); System.out.println(element.size()); } }
主函式test.java
package com.yiibai;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
/**
* Hello world!
*
*/
public class test
{
public static void main( String[] args ) throws InterruptedException
{
myThread th1 = new myThread("A"); // 開啟5個執行緒 等同於開啟5個瀏覽器 實時監聽
myThread th2 = new myThread("B");
myThread th3 = new myThread("C");
myThread th4 = new myThread("D");
myThread th5 = new myThread("E");
th1.start();
th2.start();
th3.start();
th4.start();
th5.start();
}
}
在搶購前5分鐘將test.java開啟,一直等待吧,祝你好運。
參考文獻:
2:seleniumwebdriver