肺炎实时信息爬取网页可视化

本文分享了一位大学生使用Java爬虫技术抓取COVID-19全球疫情数据的过程,并将其整合到一个自建的网页中进行可视化展示,包括数据解析、数据库操作及网页前端展示等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2020年肺炎是很受大家关注的一个问题。作为一个大三党,从肺炎刚开始那会儿就准备爬取数据并且做一个网页,奈何菜狗平日多有懒惰,一直拖到最近才把这个项目弄完。

为了弄这个网页,我特地跑去学了java爬虫,网页相关的jsp js echart servlet,这期间花费了不少时间,最后,也成功把项目弄出来了!!倍有成就感。

获取网页数据

 			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);//把读到的数据全部加到str中 以便后续分析
            }

获取世界各国家数据

 public List<Country> GetWorldResult(String str) {
        List<Country> countries= new ArrayList<>();
        String[] countryResult = str.split("\"countryType\":2,|window.getTimelineService2 ");//获取各个国家的信息
        for (int i = 1; i < countryResult.length - 1; i++) {//每个国家的数据 头尾需要去掉
            //细分数据
            Country country = new Country();
            String regex = "\"continents\":\"|\",\"provinceId\":\"[0-9]*\",\"provinceName\":\"|\",\"provinceShortName\":\"\",\"cityName\":\"\",\"currentConfirmedCount\":|,\"confirmedCount\":|,\"suspectedCount\":[0-9]+,\"curedCount\":|,\"deadCount\":|,\"deadRate\":\"[0-9]+.[0-9]+\",\"|,\"confirmedCountRank\":[0-9]+|,\"deadCountRank\":[0-9]+";
            String[] countryInformation = countryResult[i].split(regex);
            try {
                country.setContinent(countryInformation[1]);
                country.setCountryName(countryInformation[2]);
                country.setCurrentConfirmedCount(Integer.parseInt(countryInformation[3]));
                country.setConfirmedCount(Integer.parseInt(countryInformation[4]));
                if (countryInformation[5].equals("")) {
                    country.setCuredCount(Integer.parseInt(countryInformation[6]));
                    country.setDeadCount(Integer.parseInt(countryInformation[7]));
                } else {
                    country.setCuredCount(Integer.parseInt(countryInformation[5]));
                    country.setDeadCount(Integer.parseInt(countryInformation[6]));
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            countries.add(country);
        }
        return countries;
    }

接下来就是把爬到的数据写入数据库

package service;

import spiderdao.WorldDao;
import spiderentity.Country;

import java.util.Iterator;
import java.util.List;

public class CountryService {
    public boolean addCountry(List<Country> countries)//增加国家信息
    {
        WorldDao worldDao=new WorldDao();
        Iterator<Country> iterator =countries.iterator();
        while (iterator.hasNext())
            if (!worldDao.insertCountry(iterator.next()))
                return false;
            return  true;
    }
    public boolean updateAllCountry(List<Country> countries)//更新国家信息
    {
        WorldDao worldDao= new WorldDao();
        if(!worldDao.clearAllCountry())
            return false;
        return addCountry(countries);
    }

}

世界各国信息前台代码

<%@ page import="org.covid.entity.Country" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Iterator" %><%--
  Created by IntelliJ IDEA.
  User: 29057
  Date: 2020/5/5
  Time: 22:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>世界疫情</title>
    <script src="js/echarts.js"></script>
</head>
<body>
<script>
    var mydata=[]
</script>
<%
//前台代码和后台的交互
    List<Country> countryList= (List<Country>)request.getAttribute("countryList");
    Iterator<Country> iterator=countryList.iterator();

       while(iterator.hasNext())
    {
        Country country=iterator.next();
%>
<script>
    var countryName="<%=country.getCountryName()%>";
    var values=[];
    values.push("<%=country.getCurrentConfirmedCount()%>")
    values.push("<%=country.getConfirmedCount()%>")
    values.push("<%=country.getCuredCount()%>")
    values.push("<%=country.getDeadCount()%>")
    mydata.push({name:countryName,value:values})
</script>
<%
    }
%>
<img src="https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3224637141,3669251838&fm=15&gp=0.jpg" width='100%' height='30%' >
<a href="index.jsp">首页</a>
<a href="ChinaServlet">中国疫情</a>
<a href="https://new.qq.com/omn/20200310/20200310A066S000.html?pc">防疫常识</a>
<a href="https://www.baidu.com/index.php?tn=monline_3_dg">百度</a>

<br/><br/><br/>
<div id="pieChart" style="width:100%;height:100%;"></div>
<br/><br/><br/>

<script>
    option = {
        title: {
            text: '世界疫情',
            left: 'center'
        },
        tooltip: {
            trigger: 'item',
            formatter(params) {
                return params.name + '<br/>'
                    + '现有确诊 : ' +params.data.value[0]+'<br/>'
                    + '累计确诊 : ' + params.data.value[1] + '<br/>'
                    + '治愈人数 : ' + params.data.value[2] + '<br/>'
                    + '死亡人数 : ' + params.data.value[3] + '<br/>'
            },
        },
        legend: {
            orient: 'vertical',
            left: 'left',
        },
        series: [
            {
                type: 'pie',
                radius: '70%',
                center: ['70%', '60%'],
                data: mydata,
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                },
            }
        ]
    };
    var chart = echarts.init(document.getElementById('pieChart'));
    chart.setOption(option);
</script>


<table border="1px"  align="center"  cellpadding="40" bgcolor="#faebd7" >
    <tr>
        <th>国家</th>
        <th>所属大洲</th>
        <th>现有确诊</th>
        <th>累计确诊</th>
        <th>治愈人口</th>
        <th>死亡人口</th>
    </tr>

    <%
        for (Country country : countryList) {
    %>
    <tr>
        <td><%=country.getCountryName() %></td>
        <td><%=country.getContinent() %></td>
        <td><%=country.getCurrentConfirmedCount() %></td>
        <td><%=country.getConfirmedCount() %></td>
        <td><%=country.getCuredCount() %></td>
        <td><%=country.getDeadCount() %></td>
    </tr>

    <%
        }
    %>

</table>

</body>
</html>

世界后台Servlet代码

@javax.servlet.annotation.WebServlet("/WorldServlet")

public class WorldServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");//设置编码
        CountryService countryService = new CountryService();
        List<Country> countryList=countryService.QueryAllCountry();//通过countryservice服务获得数据
        request.setAttribute("countryList",countryList);//把数据放到request域
        request.getRequestDispatcher("world.jsp").forward(request, response);//请求转发
    }
}

Countryservice代码

package org.covid.service;

import org.covid.dao.WorldDao;
import org.covid.entity.Country;
import java.util.Iterator;
import java.util.List;
public class CountryService {
    WorldDao worldDao=new WorldDao();
    //其他服务暂时没写
    public List<Country> QueryAllCountry()
    {
        return worldDao.queryAllCountry();
    }
}

话不多说,直接上运行的一些效果图

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

最后就是调用数据库了,详细代码和遇到的问题都已近上传到了我的github,感兴趣的朋友可以下载来看看
爬取数据https://github.com/AndreyYLJ/Covid-19Spider
网页展示https://github.com/AndreyYLJ/Covid-19Project

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值