06-移动端疫情展示
代码实现
Web端(包含前两阶段代码)
Info.java:
package Bean;
public class Info {
private int id;
private String city;
private String yisi_num;
private String date;
private String province;
private String confirmed_num;
private String cured_num;
private String dead_num;
private String newconfirmed_num;
public String getNewconfirmed_num() {
return newconfirmed_num;
}
public void setNewconfirmed_num(String newconfirmed_num) {
this.newconfirmed_num = newconfirmed_num;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getYisi_num() {
return yisi_num;
}
public void setYisi_num(String yisi_num) {
this.yisi_num = yisi_num;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getConfirmed_num() {
return confirmed_num;
}
public void setConfirmed_num(String confirmed_num) {
this.confirmed_num = confirmed_num;
}
public String getCured_num() {
return cured_num;
}
public void setCured_num(String cured_num) {
this.cured_num = cured_num;
}
public String getDead_num() {
return dead_num;
}
public void setDead_num(String dead_num) {
this.dead_num = dead_num;
}
}
Paqu.java(如同它的名字,用来爬取数据的)
package control;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.nodes.Element;import org.jsoup.select.Elements;
import com.gargoylesoftware.htmlunit.BrowserVersion;import com.gargoylesoftware.htmlunit.WebClient;import com.gargoylesoftware.htmlunit.html.HtmlPage;import Dao.AddService;
public class Paqu {
public static void main(String args[]) {
refesh();
}
public static void refesh() {
// TODO Auto-generated method stub
String sheng="";
String xinzeng="";
String leiji="";
String zhiyu="";
String siwang="";
String country="";
char kind;
String url = "https://wp.m.163.com/163/page/news/virus_report/index.html?_nw_=1&_anw_=1";
int i=0;
try {
//构造一个webClient 模拟Chrome 浏览器
WebClient webClient = new WebClient(BrowserVersion.CHROME);
//支持JavaScript
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setActiveXNative(false);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setTimeout(8000);
HtmlPage rootPage = webClient.getPage(url);
//设置一个运行JavaScript的时间
webClient.waitForBackgroundJavaScript(6000);
String html = rootPage.asXml();
Document doc = Jsoup.parse(html);
//System.out.println(doc);
//爬取国内各省数据
Element listdiv1 = doc.select(".wrap").first();
Elements listdiv2 = listdiv1.select(".province");
for(Element s:listdiv2) {
Elements span = s.getElementsByTag("span");
Elements real_name=span.select(".item_name");
Elements real_newconfirm=span.select(".item_newconfirm");
Elements real_confirm=span.select(".item_confirm");
Elements real_dead=span.select(".item_dead");
Elements real_heal=span.select(".item_heal");
sheng=real_name.text();
xinzeng=real_newconfirm.text();
leiji=real_confirm.text();
zhiyu=real_heal.text();
siwang=real_dead.text();
System.out.println(sheng+" 新增确诊:"+xinzeng+" 累计确诊:"+leiji+" 累计治愈:"+zhiyu+" 累计死亡:"+siwang);
Date currentTime=new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time = formatter.format(currentTime);//获取当前时间
kind='1';//1代表国内省份,2代表海外,为国内外分开查询做基础
AddService dao=new AddService();
dao.add("myinfo", sheng, xinzeng, leiji, zhiyu, siwang,time,kind);//将爬取到的数据添加至数据库,注意需将“myinfo”修改为你的表名 }
//爬取海外数据
Element listdiv11 = doc.getElementById("world_block");
Elements listdiv22 =listdiv11.select(".chart_table_nation");
for(Element s:listdiv22) {
Elements real_name=s.select(".chart_table_name");
Elements real_newconfirm=s.select(".chart_table_today_confirm");
Elements real_confirm=s.select(".chart_table_confirm");
Elements real_dead=s.select(".chart_table_dead");
Elements real_heal=s.select(".chart_table_heal");
country=real_name.text();
xinzeng=real_newconfirm.text();
leiji=real_confirm.text();
zhiyu=real_heal.text();
siwang=real_dead.text();
System.out.println(country+" 新增确诊:"+xinzeng+" 累计确诊:"+leiji+" 累计治愈:"+zhiyu+" 累计死亡:"+siwang);
Date currentTime=new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time = formatter.format(currentTime);//获取当前时间
kind='2';//1代表国内省份,2代表海外,为国内外分开查询做基础
AddService dao=new AddService();
dao.add("myinfo", country, xinzeng, leiji, zhiyu, siwang,time,kind);//将爬取到的数据添加至数据库,注意需将“myinfo”修改为你的表名 }
} catch (IOException e) {
// TODO Auto-generated catch block e.printStackTrace();
System.out.println("爬取失败");
}
}
}
AddService.java(上面的Paqu.java在爬取中调用了该类,将数据添加到数据库中)
package Dao;
import java.sql.Connection;import java.sql.Statement;
import utils.DBUtil;
public class AddService {
public void add(String table,String sheng,String xinzeng,String leiji,String zhiyu,String dead,String time,char kind) {
String sql = "insert into "+table+" (Province,Newconfirmed_num ,Confirmed_num,Cured_num,Dead_num,Time,Kind) values('" + sheng + "','" + xinzeng +"','" + leiji +"','" + zhiyu + "','" + dead+ "','" + time+ "','" + kind+ "')";
System.out.println(sql);
Connection conn = DBUtil.getConn();
Statement state = null;
int a = 0;
try {
state = conn.createStatement();
a=state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(state, conn);
}
}
}
DeleteService.java(按需删除数据库中的数据,当重新爬取更新今日数据时调用)
package Dao;
import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;
import utils.DBUtil;
public class DeleteService {
public boolean delete(String table,String value)
{
boolean c=false;
Connection conn=DBUtil.getConn();
PreparedStatement state=null;
String sql="delete from "+table+" where date(Time) =?";//date(Time)将数据库表中Time转换为只有日期的形式
try {
state=conn.prepareStatement(sql);
state.setString(1,value);
int num = state.executeUpdate();
if(num!=0)
{
c= true;
}
state.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block e.printStackTrace();
}
return c;
}
}
Get.java(SearchServlet查询表中数据时调用并以List形式返回)
package Dao;
import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;
import Bean.Info;import utils.DBUtil;
public class Get {
//查询国内各省数据
public List<Info> listAll(String date1,String date2) {
ArrayList<Info> list = new ArrayList<>();
Connection conn=DBUtil.getConn();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql="select * from myinfo where Kind ='1' and Time between ? and ?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, date1);
pstmt.setString(2, date2);
rs = pstmt.executeQuery();
while (rs.next()) {
Info yq = new Info();
yq.setId(rs.getInt(1));
yq.setDate(rs.getString(8));
yq.setProvince(rs.getString(2));
yq.setNewconfirmed_num(rs.getString(3));
yq.setConfirmed_num(rs.getString(4));
yq.setCured_num(rs.getString(6));
yq.setDead_num(rs.getString(7));
list.add(yq);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return list;
}
//查询海外数据
public List<Info> listAll2(String date1,String date2) {
ArrayList<Info> list = new ArrayList<>();
Connection conn=DBUtil.getConn();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql="select * from myinfo where Kind ='2' and Time between ? and ?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, date1);
pstmt.setString(2, date2);
System.out.println(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
Info yq = new Info();
yq.setId(rs.getInt(1));
yq.setDate(rs.getString(8));
yq.setProvince(rs.getString(2));
yq.setNewconfirmed_num(rs.getString(3));
yq.setConfirmed_num(rs.getString(4));
yq.setCured_num(rs.getString(6));
yq.setDead_num(rs.getString(7));
list.add(yq);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return list;
}
}
Select.java(查询表中是否有今日数据从而判断是否删除.....现在发现根本不需要该方法,直接删除即可,不需要判断表中有没有数据)
package Dao;
import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;import java.util.ArrayList;import java.util.List;
import utils.DBUtil;
public class Select {
public boolean select(String time) {
// TODO Auto-generated method stub
Connection conn=DBUtil.getConn();
PreparedStatement pstmt = null;
ResultSet rs = null;
boolean b=false;
String sql="select * from myinfo where date(Time) = ?";
System.out.println(sql);
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, time);
rs = pstmt.executeQuery();
while (rs.next()) {
b=true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return b;
}
}
PaquServlet.java(这阶段新建的,专门用来接收移动端爬取请求的)
package Servlet;
import java.io.IOException;import java.sql.ResultSet;import java.sql.SQLException;import java.text.SimpleDateFormat;import java.util.Date;
import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import Dao.DeleteService;import control.Paqu;import utils.DBUtil;
@WebServlet("/PaquServlet")//移动端爬取用到了该Servletpublic class PaquServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("request--->"+request.getRequestURL()+"===="+request.getParameterMap().toString());
response.setContentType("text/html;charset=utf-8");
Date currentTime=new Date();
SimpleDateFormat formatter_date = new SimpleDateFormat("yyyy-MM-dd");
String date=formatter_date.format(currentTime);
DeleteService ds=new DeleteService();
ds.delete("myinfo", date);
Paqu pq=new Paqu();
pq.refesh();
}
}
SearchServlet.java(也是这阶段新建的,用来接收移动端的查找请求)
package Servlet;
import java.io.IOException;import java.sql.ResultSet;import java.sql.SQLException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.List;
import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import Bean.Info;import Dao.DeleteService;import Dao.Get;import Dao.Select;import control.Paqu;import utils.DBUtil;
@WebServlet("/SearchServlet")//移动端用到了该Servletpublic class SearchServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("request--->"+request.getRequestURL()+"===="+request.getParameterMap().toString());
response.setContentType("text/html;charset=utf-8");
String method = request.getParameter("method");
String date1 = request.getParameter("username"); // 获取客户端传过来的参数,移动端的参数叫username与password,我没有修改,可以修改为易于理解的date1,date2,但移动端也要对应修改
String date2 = request.getParameter("password");
Get get=new Get();
List<Info> list=null;
if(method.equals("province")) {//查询中国省份疫情数据
list = get.listAll(date1,date2);
}else
if(method.equals("country")) {//查询海外疫情数据
list = get.listAll2(date1, date2);
}
request.setAttribute("list",list);
Gson gson = new Gson();
String json = gson.toJson(list);
try {
response.getWriter().println(json);
// 将json数据传给客户端
} catch (Exception e) {
e.printStackTrace();
} finally {
response.getWriter().close(); // 关闭这个流,不然会发生错误的 }
}
}
YqSearch.java(前两个阶段中Web端使用的,移动端没有调用该Servlet)
package Servlet;
import java.io.IOException;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.List;
import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;
import com.google.gson.Gson;
import Bean.Info;import Dao.DeleteService;import Dao.Get;import Dao.Select;import control.Paqu;
/**
* Servlet implementation class SearchConfirmedServlet
*/
@WebServlet("/YqServlet")public class YqServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
Get get=new Get();
/**
* @see HttpServlet#HttpServlet()
*/
public YqServlet() {
super();
// TODO Auto-generated constructor stub }
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method");
if(method.equals("getAllProvince")) {
try {
getAllProvince(request, response);
} catch (ParseException e) {
// TODO Auto-generated catch block e.printStackTrace();
}
}else if(method.equals("getAllConfirmed")) {
getAllConfirmed(request, response);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub doGet(request, response);
}
protected void getAllProvince(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
response.setCharacterEncoding("UTF-8");
Select s=new Select();
Date currentTime=new Date();
SimpleDateFormat formatter_date = new SimpleDateFormat("yyyy-MM-dd");
String date=formatter_date.format(currentTime);
String date1 = request.getParameter("date1");
String date2 = request.getParameter("date2");
Date today=formatter_date.parse(date);//将现在的date转为日期,方便比较
Date date22=formatter_date.parse(date2);//将date2转为日期,方便比较
if(today.before(date22)) {//如果今天日期today比查询后边的date2日期早,需要用到今天的数据
//不管数据库中有没有今天的数据,运行到这都需要重新爬取一遍,防止官方更新今日数据
boolean b=s.select(date);//查找数据库中是否存在今天的数据.............黄色部分可删除,前面说到了,用不到查询判断表中是否有今日数据,直接删除就好,反正下面会重新爬取存到数据库
if(b) {//如果有今日数据已存在,先删除
DeleteService ds=new DeleteService();
ds.delete("myinfo", date);
}
Paqu pq=new Paqu();//不管数据库是否存在今日数据都会爬取;如果存在,前面已经删除过了,这里的爬取就相当于更新了 pq.refesh();
}
List<Info> list = get.listAll(date1,date2);
request.setAttribute("list",list);
request.setAttribute("date1",date1);
request.setAttribute("date2",date2);
request.getRequestDispatcher("bar.jsp").forward(request, response);
}
protected void getAllConfirmed(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
String date1 = request.getParameter("date1");
String date2 = request.getParameter("date2");
System.out.println(date1);
System.out.println(date2);
List<Info> list = get.listAll(date1,date2);
HttpSession session = request.getSession();
session.setAttribute("list",list);
Gson gson = new Gson();
String json = gson.toJson(list);
response.getWriter().write(json);
}
}
DBUtil.java
package utils;
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;
public class DBUtil {
public static String db_url = "jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT";//如果发布到云服务器就将localhost改为云服务器的ip public static String db_user = "root";
public static String db_pass = "root";
public static Connection getConn () {
Connection conn = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(db_url, db_user, db_pass);
} catch (Exception e) {
e.printStackTrace();
}
return 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();
}
}
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Insert title here</title><link href="{pageContext.request.contextPath }/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet"><script src="{pageContext.request.contextPath }/js/jquery-3.3.1.min.js"></script><script src="${pageContext.request.contextPath }/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<style type="text/css">
.skyblue{
background:skyblue;
}
.pink{
background:pink;
}
*{
margin:0px;
padding:0px;
}
a{
font-size:15px;
}
</style></head><body>
<div class="container">
<form action="YqServlet?method=getAllProvince" method="post">
<div class="row" style="padding-top: 20px">
<div class="col-xs-4">
<h4>起始时间:</h4>
<input type="text" class="form-control" name="date1">
</div>
<div class="col-xs-4">
<h4>终止时间:</h4>
<input type="text" class="form-control" name="date2">
</div>
<div class="col-xs-2">
<input type="submit" class="btn btn-default" value="查询">
</div>
</div>
</form>
</div></body></html>
bar.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title><link href="{pageContext.request.contextPath }/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet"><script src="{pageContext.request.contextPath }/js/jquery.min.js"></script><script src="{pageContext.request.contextPath }/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script><script type="text/javascript" src="
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步