工作中应客户需要爬取该公司股价,由于客户隐私,所以隐藏了url和参数
public class GetStockUtil {
/**
*
* @return
/
public Map getAStock(){
String url = " ";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type","application/json");
MultiValueMap<String,String> requestBody = new LinkedMultiValueMap<String, String>();
//请求体,设置请求条件
requestBody.add("q","");
requestBody.add("r","");
ResponseEntity
String stockInfo = response.getBody();
String regex = "(?<=").
//将规则封装为对象
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(stockInfo);
Map map = new HashMap();
while (m.find()) {
String str = m.group();
String[] nums = str.split("~");
map.put("股价",nums[3]);
map.put("实时变化",nums[31]);
map.put("变化率",nums[32]);
}
return map;
}
/**
* 港股实时数据
* @return 接口数据
/
public Map getHKStock(){
String url = " ";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type","application/json");
MultiValueMap<String,String> requestBody = new LinkedMultiValueMap<String, String>();
//请求体,设置请求条件
requestBody.add("q"," ");
requestBody.add("r"," ");
ResponseEntity
String stockInfo = response.getBody();
String regex = "(?<=").
//将规则封装为对象
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(stockInfo);
Map map = new HashMap();
while (m.find()) {
String str = m.group();
String[] nums = str.split("~");
map.put("股价",nums[3]);
map.put("实时变化",nums[31]);
map.put("变化率",nums[32]);
}
return map;
}
浙公网安备 33010602011771号