whatshot穷人买金 java实现监控京东积存金价格

hik
@Ta 2020-12-28发布,2020-12-29修改 31277点击

更新:程序做了优化,页面只加载一次,第三、四个参数可以忽略,默认间隔2s,可以改第三个参数设置间隔时间,电脑没网不会报错。下载地址已更新至最新。
Pc下载地址:https://hik.lanzous.com/iee3Rjt4fhe
手机app下载地址:https://hik.lanzous.com/iK7ntjuaopi

IMG_20201229_201234.png

我监控391.7买了一千块的试试,现在过了十分钟盈利6元,去掉手续费净赚3元
这是我开发的一款java黄金监控程序,控制台打开可以一直输出当前黄金价,如图:

image-20201228153932250

命令:

java -jar goldNotice-1.0-SNAPSHOT-jar-with-dependencies.jar 400 391.6 2500 3000

第一个参数是我想卖出的金价,第二个参数是买入金价,现在我还想蹲一波391.6的所以我设置的391.6,第三个参数循环间隔时间,单位毫秒,如果网络不好可能会报错,那就再加第四个参数,例如3000,就是请求页面后等待3000毫秒再获取价格(因为网速慢时间段价格会刷不出来)。
程序监控到低于买入价格或者高于卖出价格会有弹窗提示,并且会结束监控,如果部署在服务器的话可以使用Server酱微信推送。

下载地址:https://hik.lanzous.com/iee3Rjt4fhe

代码,所需依赖啥的看我这篇文章吧

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import javax.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.logging.LogManager;

public class Main {
    public static void main(String[] args) {
        LogManager.getLogManager().reset();
//        预期卖出价
        float expectPrice = 399;
//        预期买入价
        float lowPrice = 390;
//        间隔时间 默认2s
        long sec = 2000;
//        请求等待时间
        long reqSec = 5000;
        for (int i = 0; i <= 3; i++) {
            if (args.length > 0 && i == 0)
                expectPrice = Float.parseFloat(args[0]);
            if (args.length > 1 && i == 1)
                lowPrice = Float.parseFloat(args[1]);
            if (args.length > 2 && i == 2)
                sec = Long.parseLong(args[2]);
            if (args.length > 3 && i == 3)
                reqSec = Long.parseLong(args[3]);
        }
        System.out.println("数据加载中请稍候……");
        WebClient webClient = new WebClient(BrowserVersion.CHROME);//模拟火狐浏览器
        try {
            webClient.setJavaScriptTimeout(5000);
            webClient.getOptions().setUseInsecureSSL(true);//接受任何主机连接 无论是否有有效证书
            webClient.getOptions().setJavaScriptEnabled(true);//设置支持javascript脚本
            webClient.getOptions().setCssEnabled(false);//禁用css支持
            webClient.getOptions().setThrowExceptionOnScriptError(false);//js运行错误时不抛出异常
            webClient.getOptions().setTimeout(100000);//设置连接超时时间
            webClient.getOptions().setDoNotTrackEnabled(false);
            HtmlPage page = webClient.getPage("https://m.jdjygold.com/finance-gold/newgold/index/?ordersource=2&jrcontainer=h5&jrlogin=true&utm_source=Android_url_1609033920704&utm_medium=jrappshare&utm_term=wxfriends");
//                加载js需要时间故延迟默认5s
            Thread.sleep(reqSec);
            while (true) {
//                使用Jsoup解析html
                Document document = Jsoup.parse(page.asXml());
                Elements span01 = document.getElementsByClass("price_span01");
                String price = span01.text();
                String localeString = new Date().toLocaleString();
                System.out.println(localeString + " " + price);
                String msg = localeString + "价格已达到预定值:" + price;
                String lowMsg = localeString + "价格已低于预定值:" + price;
                float goldPrice = Float.parseFloat(price);
                if (msg != null && goldPrice >= expectPrice) {
                    JOptionPane.showConfirmDialog(null, msg, "达到预期卖出价提示", JOptionPane.CLOSED_OPTION);
//                若部署在服务器上则使用Server酱提醒
//                getUrl("https://sc.ftqq.com/xxxxxxxxxxxxxx.send?text="+msg);
                    return;
                }
                if (msg != null && goldPrice <= lowPrice) {
                    JOptionPane.showConfirmDialog(null, lowMsg, "低于预期买入价提示", JOptionPane.CLOSED_OPTION);
//                getUrl("https://sc.ftqq.com/xxxxxxxxxxxxxx.send?text="+lowMsg);
                    return;
                }
                Thread.sleep(sec);
            }
        } catch (FailingHttpStatusCodeException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (MalformedURLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } finally {
            webClient.close();
        }
    }

    public static void getUrl(String toUrl) throws IOException {
        URL url = new URL(toUrl);
        //建立连接
        URLConnection urlConnection = url.openConnection();
        //连接对象类型转换
        HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
        //设定请求方法
        httpURLConnection.setRequestMethod("GET");
        //获取字节输入流信息
        InputStream inputStream = httpURLConnection.getInputStream();
        //字节流转换成字符流
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
        //把字符流写入到字符流缓存中
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line = null;
        //读取缓存中的数据
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        //关闭流
        inputStream.close();
    }
}

https://cway.top

回复列表(24|隐藏机器人聊天)
添加新回复
回复需要登录