下载地址:https://www.pan38.com/share.php?code=JCnzE 提取码:8888 【仅供学习】
该Java项目实现了完整的商品价格监控系统,包含价格抓取、数据库存储、自动点击和邮件提醒功能。系统会定期检查商品价格,当价格达到预设目标时自动执行点击购买操作。
import java.sql.;
import java.time.;
public class PriceDatabase {
private static final String DB_URL = "jdbc:sqlite:prices.db";
private Connection conn;
public PriceDatabase() {
try {
Class.forName("org.sqlite.JDBC");
this.conn = DriverManager.getConnection(DB_URL);
initializeDatabase();
} catch (Exception e) {
throw new RuntimeException("数据库初始化失败", e);
}
}
private void initializeDatabase() throws SQLException {
String sql = "CREATE TABLE IF NOT EXISTS price_history (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"item_name TEXT NOT NULL," +
"price REAL NOT NULL," +
"purchase_time DATETIME DEFAULT CURRENT_TIMESTAMP," +
"url TEXT NOT NULL)";
try (Statement stmt = conn.createStatement()) {
stmt.execute(sql);
}
}
public void recordPurchase(MonitorTarget target, double price) {
String sql = "INSERT INTO price_history (item_name, price, url) VALUES (?, ?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, target.getName());
pstmt.setDouble(2, price);
pstmt.setString(3, target.getUrl());
pstmt.executeUpdate();
} catch (SQLException e) {
System.err.println("记录购买信息失败: " + e.getMessage());
}
}
public List<PurchaseRecord> getPurchaseHistory() {
List<PurchaseRecord> records = new ArrayList<>();
String sql = "SELECT * FROM price_history ORDER BY purchase_time DESC";
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
records.add(new PurchaseRecord(
rs.getString("item_name"),
rs.getDouble("price"),
rs.getTimestamp("purchase_time").toLocalDateTime(),
rs.getString("url")
));
}
} catch (SQLException e) {
System.err.println("查询购买历史失败: " + e.getMessage());
}
return records;
}
public void close() {
try {
if (conn != null) conn.close();
} catch (SQLException e) {
System.err.println("关闭数据库连接失败: " + e.getMessage());
}
}
public static class PurchaseRecord {
private final String itemName;
private final double price;
private final LocalDateTime purchaseTime;
private final String url;
public PurchaseRecord(String itemName, double price,
LocalDateTime purchaseTime, String url) {
this.itemName = itemName;
this.price = price;
this.purchaseTime = purchaseTime;
this.url = url;
}
// Getter方法...
}
}
org.openqa.selenium.;
import org.openqa.selenium.chrome.;
import org.openqa.selenium.support.ui.*;
public class ClickAutomation {
private WebDriver driver;
public ClickAutomation() {
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
this.driver = new ChromeDriver(options);
}
public void performClick(MonitorTarget target) {
try {
driver.get(target.getUrl());
// 等待页面加载
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(
By.cssSelector(".buy-button")));
// 执行点击
WebElement buyButton = driver.findElement(By.cssSelector(".buy-button"));
buyButton.click();
System.out.println("成功点击购买按钮: " + target.getName());
} catch (Exception e) {
System.err.println("自动点击失败: " + e.getMessage());
}
}
public void close() {
if (driver != null) {
driver.quit();
}
}
}
javax.mail.;
import javax.mail.internet.;
import java.util.Properties;
public class PriceAlertService {
private final String smtpHost;
private final String username;
private final String password;
public PriceAlertService(String smtpHost, String username, String password) {
this.smtpHost = smtpHost;
this.username = username;
this.password = password;
}
public void sendAlert(MonitorTarget target, double currentPrice) {
Properties props = new Properties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("user@example.com"));
message.setSubject("价格提醒: " + target.getName());
String content = String.format(
"商品: %s\n当前价格: %.2f\n目标价格: %.2f\n购买链接: %s",
target.getName(), currentPrice, target.getTargetPrice(), target.getUrl());
message.setText(content);
Transport.send(message);
System.out.println("已发送价格提醒邮件");
} catch (MessagingException e) {
System.err.println("发送邮件失败: " + e.getMessage());
}
}
}