一, 题目要求
说明: 这里我只展示爬取数据的代码, 将可视化结果与统计数据结合, 实时显示当前最新数据只需将这篇博客代码和我那篇使用 Echarts 可视化数据库数据结合即可
二, 思路
同学思路: 我的大部分同学思路是使用 HttpClient+Jsoup 获取并解析目标页面的数据, 然后调用 Dao 层中的方法将数据筛选并存入数据库中
我的思路: 我直接使用了一个网上的一个最新疫情数据 JSON 的目标网页, 然后使用 HttpClient 获取该 JSON 格式数据, 然后使用 JSON 解析器将
数据分离, 然后调用 Dao 层中的方法将数据筛选并存入数据库中.
感想: 个人感觉自己懒了一点, 但感觉只要完成目的就可以了
三, 源代码
Dao.java
- package Dao;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import DBUtil.DBUtil;
- import Test.Yione;
- public class Dao {
- public boolean add(int id,String provinceName,String confirmeNum,String curesNum, String deathsNum )
- {
- Connection conn = DBUtil.getConn();
- PreparedStatement pstmt = null;
- boolean f = false;
- int a=0;
- try {
- String sql = "insert into todaydata_copy1(id,provinceName,confirmeNum,curesNum,deathsNum value(?,?,?,?,?)";
- pstmt = conn.prepareStatement(sql);
- pstmt.setInt(1, id);
- pstmt.setString(2, provinceName);
- pstmt.setString(3, confirmeNum);
- pstmt.setString(4, curesNum);
- pstmt.setString(5, deathsNum );
- a = pstmt.executeUpdate();
- }
- catch(SQLException e) {
- e.printStackTrace();
- }
- finally {
- DBUtil.close(pstmt, conn);
- }
- if(a>0)
- f=true;
- return f;
- }
- }
- View Code
- DBUtil.java(连接池代码, 每个人的都有点不同)
- package DBUtil;
- import java.sql.*;
- /**
- * 数据库连接工具
- * @author Hu
- *
- */
- public class DBUtil {
- public static String url = "jdbc:mysql://localhost:3306/yiqing?serverTimezone=UTC";
- public static String user = "root";
- public static String password = "123";
- public static Connection getConn () {
- Connection conn = null;
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");// 加载驱动
- conn = DriverManager.getConnection(url, user, password);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return conn;
- }
- /**
- * 关闭连接
- * @param state
- * @param conn
- */
- public static void close (PreparedStatement preparedState, Connection conn) {
- if (preparedState != null) {
- try {
- preparedState.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static void close (ResultSet rs, PreparedStatement preparedState, Connection conn) {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if (preparedState != null) {
- try {
- preparedState.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 关闭连接
- * @param state
- * @param conn
- */
- public static void close (Statement state, Connection conn) {
- if (state != null) {
- try {
- state.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static void close (ResultSet rs, Statement state, Connection conn) {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if (state != null) {
- try {
- state.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) throws SQLException {
- Connection conn = getConn();
- PreparedStatement preparedStatement = null;
- ResultSet rs = null;
- String sql ="select * from testr";
- preparedStatement = conn.prepareStatement(sql);
- rs = preparedStatement.executeQuery();
- if(rs.next()){
- System.out.println("数据库为空");
- }
- else{
- System.out.println("数据库不为空");
- }
- }
- }
- View Code
Javabean 部分代码
- package Test;
- public class Yione {
- private int id;
- private String provinceName;
- private String confirmeNum;
- private String curesNum;
- private String deathsNum;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getProvinceName() {
- return provinceName;
- }
- public void setProvinceName(String provinceName) {
- this.provinceName = provinceName;
- }
- public String getConfirmeNum() {
- return confirmeNum;
- }
- public void setConfirmeNum(String confirmeNum) {
- this.confirmeNum = confirmeNum;
- }
- public String getCuresNum() {
- return curesNum;
- }
- public void setCuresNum(String curesNum) {
- this.curesNum = curesNum;
- }
- public String getDeathsNum() {
- return deathsNum;
- }
- public void setDeathsNum(String deathsNum) {
- this.deathsNum = deathsNum;
- }
- }
- View Code
使用 HttpClient 获取该 JSON 格式数据部分代码
- package Test;
- import org.apache.http.client.methods.CloseableHttpResponse;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.util.EntityUtils;
- public class getEntity {
- public static String get(){
- // 创建客户端
- CloseableHttpClient httpClient = HttpClients.createDefault();
- String entityr = "";
- // 创建 Get 实例
- HttpGet httpGet = new HttpGet("http://www.dzyong.top:3005/yiqing/province");
- // 添加请求头的信息, 模拟浏览器访问
- httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0");
- try{
- // 获得 Response
- CloseableHttpResponse response = httpClient.execute(httpGet);
- if(response.getStatusLine().getStatusCode() == 200){
- // 当响应状态码为 200 时, 获得该网页源码并打印
- String entity = EntityUtils.toString(response.getEntity(),"utf-8");
- entityr = entity;
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- return entityr;
- }
- }
- View Code
解析数据并存入数据库的主函数代码
- package Test;
- import java.util.List;
- import com.alibaba.fastjson.JSON;
- import Dao.Dao;
- public class Demo {
- public static void main(String[] args){
- Dao dao=new Dao();
- String str = getEntity.get();
- int index=str.indexOf("[");
- int index2=str.indexOf("]");
- String str1 = str.substring(index-1,index2+1);
- // 字符串序列化成集合
- List<Yione> list= JSON.parseArray(str1,Yione.class);
- for(Yione item: list){
- System.out.println(item.getId() );
- System.out.println(item.getProvinceName());
- System.out.println(item.getConfirmeNum());
- System.out.println(item.getCuresNum());
- System.out.println(item.getDeathsNum());
- dao.add(item.getId(),item.getProvinceName(),item.getConfirmeNum(),item.getCuresNum(),item.getDeathsNum());
- }
- }
- }
- View Code
四, 效果展示
由于这个最后效果只是一个写满数据的数据表, 就不展示了
五, 时间展示
日期 | 开始时间 | 结束时间 | 中断时间 | 净时间 | 活动 | 备注 |
2020-3-10---2020-3-12 | 15:50 | 17:25 | 10 分钟 | 1 小时 25 分钟 | 学习网络上关于 Java 爬取的代码 & nbsp; | 简书上的代码: https://www.jianshu.com/p/fd5caaaa950d 学习博客: https://blog.csdn.net/lanxuan1/article/details/77070864 |
13:15 | 16:45 | 30 分钟 | 3 小时 | 学习使用 Jsoup | 学习地址:https://cloud.tencent.com/developer/article/1508640 | |
18:15 | 19:30 | 15 分钟 | 1 小时 | 学习 Json 解析器和使用 HttpClient | 学习博客地址: https://www.cnblogs.com/dyf-stu/p/9976241.html | |
20:15 | 22:50 | 15 分钟 | 2 小时 30 分钟 | 建数据库和数据表,编写代码 | 难以确定 Json 数据中‘[‘和‘]‘的位置,从而导致解析数据错误 | |
22:55 | 23:30 | 35 分钟 | 测试爬取数据代码并完成数据可视化 | 加上 Echarts 可视化部分代码 |
来源: http://www.bubuko.com/infodetail-3465071.html