首先需要用爬蟲,在這我是用的java進行的爬取丁香園疫情數據 學習位置 https://blog.csdn.net/qq_27471405/article/details/104140132 ,在其基礎上進行了修改以爬取全球數據) 爬取全球數據,並存到電腦上的MySQL。
package yiqing; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.security.Timestamp; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import javax.net.ssl.HttpsURLConnection; import javax.xml.crypto.Data; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; public class WuhanService { public static void main(String[] args) throws IOException { getAreaStat(); getListByCountryTypeService2(); } // 根URL private static String httpRequset(String requesturl) throws IOException { StringBuffer buffer = null; BufferedReader bufferedReader = null; InputStreamReader inputStreamReader = null; InputStream inputStream = null; HttpsURLConnection httpsURLConnection = null; try { URL url = new URL(requesturl); httpsURLConnection = (HttpsURLConnection) url.openConnection(); httpsURLConnection.setDoInput(true); httpsURLConnection.setRequestMethod("GET"); inputStream = httpsURLConnection.getInputStream(); inputStreamReader = new InputStreamReader(inputStream, "utf-8"); bufferedReader = new BufferedReader(inputStreamReader); buffer = new StringBuffer(); String str = null; while ((str = bufferedReader.readLine()) != null) { buffer.append(str); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return buffer.toString(); } /** * 獲取全國各個省市的確診、死亡和治愈人數 * * @return */ public static String getAreaStat() { String url = "https://ncov.dxy.cn/ncovh5/view/pneumonia"; String htmlResult = ""; try { htmlResult = httpRequset(url); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // System.out.println(htmlResult); // 正則獲取數據 // 因為html的數據格式看着就像json格式,所以我們正則獲取json String reg = "window.getAreaStat = (.*?)\\}(?=catch)"; Pattern totalPattern = Pattern.compile(reg); Matcher totalMatcher = totalPattern.matcher(htmlResult); String result = ""; if (totalMatcher.find()) { result = totalMatcher.group(1); System.out.println(result); // 各個省市的是一個列表List,如果想保存到數據庫中,要遍歷結果,下面是demo JSONArray array = JSONArray.parseArray(result); DBUTIL l = new DBUTIL(); try { Connection con = l.lin("VData"); Statement stmt = con.createStatement(); Date date = new Date();//獲得系統時間. SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss" ); String nowTime = sdf.format(date); for (int i = 0; i <= 30; i++) { com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject .parseObject(array.getString(i)); String provinceName = jsonObject.getString("provinceName"); String cityname1 = " "; String confirmed = jsonObject.getString("confirmedCount"); String cured = jsonObject.getString("curedCount"); String dead = jsonObject.getString("deadCount"); String suspect = jsonObject.getString("suspectedCount"); stmt.executeUpdate("insert into info2(Date,Province,City,Confirmed_num,Yisi_num,Cured_num,Dead_num) values('"+ nowTime + "','"+ provinceName + "','"+ cityname1 + "','" + confirmed + "','" + suspect +"','" + cured +"','" + dead +"')"); JSONArray array2 = jsonObject.getJSONArray("cities"); for (int j = 0; j < array2.size(); j++) { com.alibaba.fastjson.JSONObject jsonObject2 = com.alibaba.fastjson.JSONObject .parseObject(array2.getString(j)); String provinceName2 = jsonObject.getString("provinceName"); String cityname = jsonObject2.getString("cityName"); String confirmed2 = jsonObject2.getString("confirmedCount"); String cured2 = jsonObject2.getString("curedCount"); String dead2 = jsonObject2.getString("deadCount"); String suspect2 = jsonObject2.getString("suspectedCount"); stmt.executeUpdate("insert into info3(Date,Province,City,Confirmed_num,Yisi_num,Cured_num,Dead_num) values('"+ nowTime + "','"+ provinceName2 + "','"+ cityname + "','" + confirmed2 + "','" + suspect2 +"','" + cured2 +"','" + dead2 +"')"); } } stmt.close(); con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return result; } /** * 獲取全球各個國家的確診、死亡和治愈人數 * @return */ public static String getListByCountryTypeService2() { String url = "https://ncov.dxy.cn/ncovh5/view/pneumonia"; String htmlResult = ""; try { htmlResult = httpRequset(url); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // System.out.println(htmlResult); // 正則獲取數據 // 因為html的數據格式看着就像json格式,所以我們正則獲取json String reg = "window.getListByCountryTypeService2true = (.*?)\\}(?=catch)"; Pattern totalPattern = Pattern.compile(reg); Matcher totalMatcher = totalPattern.matcher(htmlResult); String result = ""; if (totalMatcher.find()) { result = totalMatcher.group(1); System.out.println(result); // 各個省市的是一個列表List,如果想保存到數據庫中,要遍歷結果,下面是demo JSONArray array = JSONArray.parseArray(result); DBUTIL l = new DBUTIL(); try { Connection con = l.lin("VData"); Statement stmt = con.createStatement(); Date date = new Date();//獲得系統時間. SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss" ); String nowTime = sdf.format(date); for (int i = 0; i <array.size(); i++) { com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject .parseObject(array.getString(i)); String continents =jsonObject.getString("continents"); String provinceName = jsonObject.getString("provinceName"); String confirmed = jsonObject.getString("confirmedCount"); String cured = jsonObject.getString("curedCount"); String dead = jsonObject.getString("deadCount"); String suspect = jsonObject.getString("suspectedCount"); stmt.executeUpdate("insert into info4(Date,Continents,Province,Confirmed_num,Yisi_num,Cured_num,Dead_num) values('"+ nowTime + "','"+ continents + "','"+ provinceName + "','" + confirmed + "','" + suspect +"','" + cured +"','" + dead +"')"); } stmt.close(); con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return result; } }
然后通過調取數據庫數據進行圖標和列表展示,效果如圖。
移動端連接遠程數據庫實現數據查詢展示
日期 | 起始時間 | 結束時間 | 活動 | 備注 |
3.17 | 14:00 | 15:30 | 聽課 | |
15:40 | 18:00 | 編程 | 爬取數據並入庫 | |
19:00 | 20:30 | 看視頻 | 學習雲服務器的構建 | |
3.18 | 13:00 | 18:00 | 學習+跟做 | 一邊看視頻一變創建自己的服務器 |
19:00 | 20:00 | 配置服務器 | 未完成配置 | |
3.19 | 14:30 | 15:30 | 配置服務器 | 完成基本配置與訪問 |
15:50 | 18:30 | 安裝數據庫 | 失敗 | |
19:30 | 20:00 | 學習 | 看視頻,決定放棄雲服務器 | |
3.20 | 19:00 | 22:00 | 配環境 | 內網穿透實現其他客戶端的訪問 |
3.21 | 7:00 | 10:00 | 編程 | 編寫app項目 |
10:10 | 11:30 | 編程 | 建立app與數據庫間的鏈接 |