ybf

ybf


1.12中国招商网站汇率抓取代码参考

/**

  • 中国招商(http://fx.cmbchina.com/Hq/)银行汇率爬取
  • @author
  • @date 2021-12-15 */ @Slf4j @Component public class MerchantsBankExchangePrice{

    private static final String BASE_URL="http://fx.cmbchina.com/Hq/";;

    final static Integer BUY_DIRECTION = TradeDirectionEnum.BUY_FROM_USER.getCode();

    final static Integer SELL_DIRECTION = TradeDirectionEnum.SELL_TO_USER.getCode();

    @Value("${exchange.rate.discard.time:2000}") private long exchangeRateDiscardTime;

    @Autowired private Environment environment;

    public List<BaseExchangeRateHistoryDTO> fetchExchangeRate(){ StopWatch stopWatch=new StopWatch(); String rs =null; try { stopWatch.start(); //本段代码为测试汇率波动使用,生产不执行------BEGIN int testSleepTime = environment.getProperty("exchange.rate.test.sleep.time", Integer.class, 0); if (testSleepTime>0) { try { TimeUnit.SECONDS.sleep(testSleepTime); } catch (InterruptedException e) { e.printStackTrace(); } log.info("延迟测试时间,{}",testSleepTime); } //本段代码为测试汇率波动使用,生产不执行------END rs = HttpClientUtils.get(BASE_URL); stopWatch.stop(); }catch(Exception e){ log.error("【招商银行】汇率抓取异常,{}",e.getMessage(),e); rs=e.getMessage(); } List<BaseExchangeRateHistoryDTO> list=new ArrayList<>(2); if(StringUtils.isNotBlank(rs)){ try { Optional<Map<Integer, BigDecimal>> optional = parseHtml(rs,stopWatch); if(optional.isPresent()){ optional.get().forEach((k,v)->{ list.add(newInstance(v,k+":"+v,k, BUY_DIRECTION.equals(k)?ApiInfoEnum.CMBCHINA_CNY_USD_FIRST_BUY_PRICE:ApiInfoEnum.CMBCHINA_CNY_USD_FIRST_SELL_PRICE, stopWatch.getTotalTimeMillis())); }); } }catch (Exception e){ log.error("【招商银行】汇率抓取结果:{},结果解析异常:{}",rs,e.getMessage(),e); rs=e.getMessage(); } } if(list.isEmpty()){ list.add(newInstance(null,rs,BUY_DIRECTION, ApiInfoEnum.CMBCHINA_CNY_USD_FIRST_BUY_PRICE, exchangeRateDiscardTime+1)); list.add(newInstance(null,rs,SELL_DIRECTION,ApiInfoEnum.CMBCHINA_CNY_USD_FIRST_SELL_PRICE,exchangeRateDiscardTime+1)); } return list; }

    private static BaseExchangeRateHistoryDTO newInstance(BigDecimal price, String responseString, Integer tradeType, ApiInfoEnum apiInfo, Long delayTime ){ BaseExchangeRateHistoryDTO baseExchangeRateHistoryDTO = new BaseExchangeRateHistoryDTO(); baseExchangeRateHistoryDTO.setCoinType(AssetType.USDT); baseExchangeRateHistoryDTO.setPriceTrading(RateTradingEnum.CNY_USD); baseExchangeRateHistoryDTO.setPriceUnit(AssetType.CNY); baseExchangeRateHistoryDTO.setDuration(delayTime); baseExchangeRateHistoryDTO.setResponse(responseString); baseExchangeRateHistoryDTO.setPrice(price); baseExchangeRateHistoryDTO.setDirection(tradeType); baseExchangeRateHistoryDTO.setApiInfo(apiInfo); baseExchangeRateHistoryDTO.setApiSource(RateSourceEnum.CMBCHINA); log.info("cmbbank baseExchangeRateHistoryDTO: {}", JacksonUtils.toJson(baseExchangeRateHistoryDTO)); return baseExchangeRateHistoryDTO; }

    private static Optional<Map<Integer, BigDecimal>> parseHtml(String html,StopWatch stopWatch){ Document document = Jsoup.parse(html); Elements data = document.getElementsByClass("data"); Elements select = data.select("tbody>tr"); Optional<Map<Integer, BigDecimal>> optional = select.stream().filter(e -> "美元".equals(e.child(0).text())).map(e -> { log.info("【招商银行】汇率抓取,请求地址:{} \n爬取结果:{} \n耗时:{}ms",BASE_URL,e.html(),stopWatch.getTotalTimeSeconds()); BigDecimal baseNum=new BigDecimal(100); //卖价 BigDecimal buy=new BigDecimal(e.child(3).text()); //买价 BigDecimal sell=new BigDecimal(e.child(5).text()); Map<Integer, BigDecimal> map = new HashMap<>(2); map.put(SELL_DIRECTION, sell.divide(baseNum,6, RoundingMode.DOWN)); map.put(BUY_DIRECTION, buy.divide(baseNum,6,RoundingMode.DOWN)); return map; }).findFirst(); return optional; } }

页面列表

ITEM_HTML