# scSovid-19 **Repository Path**: wolf_ghost/sc-sovid-19 ## Basic Information - **Project Name**: scSovid-19 - **Description**: 一个使用Springboot、mybatis-plus、httpclient获取新浪新闻的疫情数据。 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-05-09 - **Last Updated**: 2022-05-09 ## Categories & Tags **Categories**: Uncategorized **Tags**: SpringBoot, MyBatis ## README # scSovid-19 #### 介绍 一个使用Springboot、mybatis-plus、httpclient获取新浪新闻的疫情数据。 ### 四川省疫情展示平台后端说明 ### 技术选型 1. Spring Boot 2. Mybatis-plus 3. FastJSON 4. Apache开源的一个http访问工具(httpclient),用于对新浪新闻的数据接口请求获取数据 5. MYSQL 6. Spring boot 定时任务,用于定时爬取数据并存入MYSQL数据库。 ### 项目启动 > 采用Maven的方式管理项目中的相关依赖,启动需要先导入相关依赖。 > 然后配置数据连接,需要在下面的配置中填写自己的数据库地址、用户名、密码。如下所示 ```yaml server: port: 8081 spring: datasource: url: jdbc:mysql://192.168.147.132:3306/sc_covid19?characterEncoding=utf-8&serverTimezone=Asia/Shanghai #数据连接地址 username: root #数据库用户名 password: 123456 #数据库密码 driver-class-name: com.mysql.cj.jdbc.Driver mybatis-plus: configuration: # 日志 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl map-underscore-to-camel-case: false #设置表字段是否开启驼峰命名,默认为true global-config: db-config: id-type: auto mapper-locations: classpath*:mapper/*Mapper.xml ``` > 数据获取相关说明 获取数据的相关代码,此段代码先通过Apache的http连接工具获取数据,然后对数据处理。首先是利用正则表达式将数据的一些多余数据去除,然后使用FastJSON处理为json格式数据,再通过工具类将其转换为JavaBean。具体代码如下: ```java public class GetDataFromSina { private final static String sina_url = "https://gwpre.sina.cn/interface/news/ncp/data.d.json?mod=province&province=sichuan&_=1652070853679&callback=_aFunction"; private static JSONObject jsonData; static { try { jsonData = getAllData(); } catch (Exception e) { throw new RuntimeException(e); } } private static JSONObject getAllData() throws Exception { String data = HttpUtils.httpToGet(sina_url); String regStr = "(\\_aFunction\\()|(\\);)"; String replace = data.replaceAll(regStr, ""); String decodeStr = UnicodeDecode.unicodeToString(replace); JSONObject jsonObject = JSONObject.parseObject(decodeStr); return jsonObject; } /** * 获取日常数据主要用于常规数据展示 * @return */ public Daily getDaily(){ return jsonData.getObject("data",Daily.class); } /** * 获取城市数据 * @return */ public List getCity(){ List cityList = new ArrayList<>(); JSONArray city = jsonData.getJSONObject("data").getJSONArray("city"); for (int i = 0; i < city.size(); i++) { System.out.println(city.get(i)); cityList.add(city.getObject(i, City.class)); } return cityList; } /** * 获取每日数据变化情况 * @return */ public Adddaily getAddDaily(){ return jsonData.getJSONObject("data").getObject("adddaily",Adddaily.class); } /** * 获取历史数据 * @return */ public List getHistoryList(){ JSONArray jsonArray = jsonData.getJSONObject("data") .getJSONArray("historylist"); List historyLists = new ArrayList<>(); for (int i = 0; i < jsonArray.size(); i++) { historyLists.add(jsonArray.getObject(i,Historylist.class)); } return historyLists; } } ``` > 关于Http 工具的封装,模拟浏览器发送请求获取数据。具体代码如下: ```java public class HttpUtils { public static String httpToGet(String baseUrl) throws Exception { //可关闭的httpClient客户端,相当于你打开的一个浏览器 CloseableHttpClient aDefault = HttpClients.createDefault(); //接受参数 String toStringResult = null; //构造请求对象 HttpGet httpGet = new HttpGet(baseUrl); //解决httpClient被认为不是真人行为 httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"); //构造响应对象 CloseableHttpResponse response = null; try { response = aDefault.execute(httpGet); //代表本次请求的成功、失败的状态,响应码为200则表示成功。 StatusLine statusLine = response.getStatusLine(); if (HttpStatus.SC_OK == statusLine.getStatusCode()) { //获取响应实体 HttpEntity entity = response.getEntity(); //对HttpEntity操作的工具类,获取返回的数据 toStringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8); //确保流关闭 EntityUtils.consume(entity); } } catch (Exception e) { e.printStackTrace(); } finally { if (aDefault != null) { try { aDefault.close(); } catch (IOException e) { e.printStackTrace(); } } if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } } return toStringResult; } } ``` > 获取的数据进行Unicode解码,具体代码如下 ```java public class UnicodeDecode { public static String unicodeToString(String str) { Pattern pattern = Pattern.compile("(\\\\u(\\w{4}))"); Matcher matcher = pattern.matcher(str); char ch; while (matcher.find()) { // 本行为核心代码,处理当前的unicode后4位变为16进制,在转换为对应的char中文字符 ch = (char) Integer.parseInt(matcher.group(2), 16); str = str.replace(matcher.group(1), ch + ""); } return str; } } ``` > 接口地址为:http://localhost:8081/data/sc 请求方式为get