ybf

ybf


1.13抹茶网站汇率抓取代码参考

@Service @Slf4j public class MexcExchangePrice implements CoinPriceFetchService { @Autowired private FetchExchangeRateService fetchExchangeRateService;

private static final String BASE_URL="https://otc.mexc.com/api/market";

@Autowired
public MexcExchangePrice(FetchExchangeRateService fetchExchangeRateService) {
    this.fetchExchangeRateService = fetchExchangeRateService;
}

public void fetchPriceJsonAndSave() {}
private final static Map<String, String> HEADERS = buildHeader();

private static Map<String, String> buildHeader() {
    Map<String, String> headers = new HashMap<>();
    headers.put("accept", "application/json");
    headers.put(":method", "GET");
    headers.put("app-type", "web");
    headers.put("accept-language", "zh-CN,zh;q=0.9");
    headers.put("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36");
    // headers.put("x-cdn", "https://static.okex.com");
    return headers;
}

public String buildParam(String buyOrSell, String assetType) {
    //https://otc.mexc.com/api/market?blockTrade=false&coinName=USDT&countryCode=&currency=CNY&page=1&payMethod=&tradeType=SELL
    StringBuilder sb = new StringBuilder();
    sb.append("?blockTrade=false");
    if (AssetType.USDT.getSymbol().equals(assetType)) {
        sb.append("&coinName=USDT");
    } else if (AssetType.ETH.getSymbol().equals(assetType)) {
        sb.append("&coinName=ETH");
    } else {
        throw new RuntimeException("查询的币种有误.");
    }
    //currency=1代表人民币
    sb.append("&countryCode=");
    sb.append("&currency=CNY");
    sb.append("&page=1");
    sb.append("&payMethod=");
    sb.append("&tradeType=").append(buyOrSell);
    log.info("抹茶网sb={}", sb.toString());
    return sb.toString();
}

public void fetchPriceJsonAndSave(String fetchId) {
    getAndSavePriceInfo("SELL", AssetType.USDT.getSymbol(), fetchId);
    getAndSavePriceInfo("BUY", AssetType.USDT.getSymbol(), fetchId);
    getAndSavePriceInfo("SELL", AssetType.ETH.getSymbol(), fetchId);
    getAndSavePriceInfo("BUY", AssetType.ETH.getSymbol(), fetchId);
}

private void getAndSavePriceInfo(String sellOrBuy, String assetType, String fetchId) {
    long start = System.currentTimeMillis();
    String responseString;
    try{
        String params = buildParam(sellOrBuy, assetType);
        responseString = HttpClientUtils.getUseProxy(BASE_URL + params, HEADERS);
        log.info("获取抹茶网{},{},币价信息:{}", assetType, sellOrBuy, responseString);
    } catch (Exception e) {
        log.error("获取抹茶网{}币价信息失败:{}", assetType, e.getMessage());
        responseString = e.getMessage();
    }
    long delayTime = System.currentTimeMillis() - start;

    if (AssetType.USDT.getSymbol().equals(assetType)) {
        //USDT信息
        parseAndSave(responseString, AssetType.USDT.getSymbol(), sellOrBuy, delayTime, fetchId);
    } else if(AssetType.ETH.getSymbol().equals(assetType)){
        //ETH信息
        parseAndSave(responseString, AssetType.ETH.getSymbol(), sellOrBuy, delayTime, fetchId);
    }

}

private void parseAndSave(String responseString, String assetType, String sellOrBuy, long delayTime, String fetchId) {
    BigDecimal price = null;
    if (StringUtils.isNotBlank(responseString)) {
        try {
            JSONArray jsonArray = JSONObject.parseObject(responseString).getJSONArray("data");
            //取第一条价格即可
            JSONObject jsonObject = jsonArray.getJSONObject(0);
            log.info("抹茶网抓取到的data(0)=:{}", jsonObject.toJSONString());
            if(StringUtils.isNotBlank(jsonObject.getString("price"))){
                price = new BigDecimal(jsonObject.getString("price"));
            }
            log.info("抹茶网抓取到的{}价格为:{}", assetType, price);
        } catch (Exception e) {
            log.error("抹茶网数据格式异常:{}", e.getMessage());
        }
    }
    try {
        FetchExchangeRatePO fetchExchangeRatePO = new FetchExchangeRatePO();
        fetchExchangeRatePO.setFetchBatches(fetchId);
        if(AssetType.USDT.getSymbol().equals(assetType)){
            fetchExchangeRatePO.setCoinType(AssetType.USDT.getCode());
        }
        if(AssetType.ETH.getSymbol().equals(assetType)){
            fetchExchangeRatePO.setCoinType(AssetType.ETH.getCode());
        }
        fetchExchangeRatePO.setPriceUnit(AssetType.CNY.getCode());
        if(sellOrBuy.equals("SELL")){
            fetchExchangeRatePO.setTradeType(TradeDirectionEnum.SELL_TO_USER.getCode());
        }
        if(sellOrBuy.equals("BUY")){
            fetchExchangeRatePO.setTradeType(TradeDirectionEnum.BUY_FROM_USER.getCode());
        }
        if(price==null){
            fetchExchangeRatePO.setPrice(new BigDecimal(0));
        }else{
            fetchExchangeRatePO.setPrice(price);
        }
        fetchExchangeRatePO.setApiName(ApiInfoConstant.MEXC_API_NAME);
        if(AssetType.USDT.getSymbol().equals(assetType)&&sellOrBuy.equals("BUY")){
            fetchExchangeRatePO.setApiInfo(ApiInfoConstant.MEXC_USDT_CNY_FIRST_SELL_PRICE);
        }
        if(AssetType.USDT.getSymbol().equals(assetType)&&sellOrBuy.equals("SELL")){
            fetchExchangeRatePO.setApiInfo(ApiInfoConstant.MEXC_USDT_CNY_FIRST_BUY_PRICE);
        }
        if(AssetType.ETH.getSymbol().equals(assetType)&&sellOrBuy.equals("BUY")){
            fetchExchangeRatePO.setApiInfo(ApiInfoConstant.MEXC_ETH_CNY_FIRST_SELL_PRICE);
        }
        if(AssetType.ETH.getSymbol().equals(assetType)&&sellOrBuy.equals("SELL")){
            fetchExchangeRatePO.setApiInfo(ApiInfoConstant.MEXC_ETH_CNY_FIRST_BUY_PRICE);
        }
        fetchExchangeRatePO.setStatus(1);
        fetchExchangeRatePO.setDelayTime(delayTime);
        log.info("mexc fetchExchangeRatePO:{}"+ JacksonUtils.toJson(fetchExchangeRatePO));
        fetchExchangeRateService.save(fetchExchangeRatePO);
    } catch (Exception e) {
        log.error("抹茶网汇率save失败:{}" + e.getMessage(), e);
    }
}

public static void main(String[] args) {
    try {
        String buyUSDTUrl="https://otc.mexc.com/api/market?blockTrade=false&coinName=USDT&countryCode=&currency=CNY&page=1&payMethod=&tradeType=BUY";
        String sellUSDTUrl="https://otc.mexc.com/api/market?blockTrade=false&coinName=USDT&countryCode=&currency=CNY&page=1&payMethod=&tradeType=SELL";
        String buyETHUrl="https://otc.mexc.com/api/market?blockTrade=false&coinName=ETH&countryCode=&currency=CNY&page=1&payMethod=&tradeType=BUY";
        String sellETHUrl="https://otc.mexc.com/api/market?blockTrade=false&coinName=ETH&countryCode=&currency=CNY&page=1&payMethod=&tradeType=SELL";

        String responseString = HttpClientUtils.getUseProxy(buyUSDTUrl, HEADERS);
        log.info(responseString);
    } catch (Exception e) {
        log.error("获取抹茶网币价信息失败:{}", e.getMessage());
    }
}

}

页面列表

ITEM_HTML