zoukankan      html  css  js  c++  java
  • 移动端和web端 全球疫情数据查询

    首先需要用爬虫,在这我是用的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与数据库间的链接
  • 相关阅读:
    java中Collection 与Collections的区别
    Bridge桥接模式(设计模式11)
    动态代理模式(设计模式10)
    代理模式(静态代理)(设计模式9)
    适配器模式(工厂模式8)
    原型模式(工厂模式7)
    建造者模式(工厂模式6)
    抽象工厂模式(工厂模式5)
    工厂模式(工厂模式4)
    简单工厂模式(工厂模式3)
  • 原文地址:https://www.cnblogs.com/dwx8845/p/12547966.html
Copyright © 2011-2022 走看看