3月10日课上作业

 

 爬虫的代码

 1 import requests
 2 import time, json
 3 import sys;
 4 import pymysql
 5 
 6 def get_wangyi_request():
 7     url = 'https://c.m.163.com/ug/api/wuhan/app/data/list-total'
 8 
 9     headers = {
10         'accept': '*/*',
11         'accept-encoding': 'gzip,deflate,br',
12         'accept-language': 'en-US,en;q=0.9,zh-CN;q = 0.8,zh;q = 0.7',
13         'origin': 'https://wp.m.163.com',
14         'referer': 'https://wp.m.163.com/',
15         'sec-fetch-dest': 'empty',
16         'sec-fetch-mode': 'cors',
17         'sec-fetch-site': 'same-ite',
18         'user-agent': 'Mozilla/5.0(WindowsNT10.0;Win64;x64) AppleWebKit/37.36 (KHTML, likeGecko) Chrome/82.0.4056.0 Safari/537.36 Edg/82.0.432.3'
19     }
20 
21     result = requests.get(url, headers=headers)
22     return result
23 
24 
25 def print_mess1(string: str, dict1total: dict):
26     sys.stdout.write(string + '确诊: ' + str(dict1total['confirm'] if dict1total['confirm'] != None else 0))
27     sys.stdout.write(' ')
28     sys.stdout.write(string + '疑似: ' + str(dict1total['suspect'] if dict1total['suspect'] != None else 0))
29     sys.stdout.write(' ')
30     sys.stdout.write(string + '治愈: ' + str(dict1total['heal'] if dict1total['heal'] != None else 0))
31     sys.stdout.write(' ')
32     sys.stdout.write(string + '死亡: ' + str(dict1total['dead'] if dict1total['dead'] != None else 0))
33 
34 
35 if __name__ == '__main__':
36     result = get_wangyi_request()
37 
38     json_str = json.loads(result.text)['data']
39     # print(json_str.keys())
40     # dict_keys(['chinaTotal', 'chinaDayList', 'lastUpdateTime', 'areaTree'])
41 
42     print(json_str['lastUpdateTime'])
43     provinae_list = json_str['areaTree'][0]['children']
44     # 每个省份包含如下的键
45     # dict_keys(['today', 'total', 'extData', 'name', 'id', 'lastUpdateTime', 'children'])
46 
47     conn = pymysql.connect(
48         host='localhost',  # 我的IP地址
49         port=3306,  # 不是字符串不需要加引号。
50         user='root',
51         password='123456',
52         db='python',
53         charset='utf8'
54     )
55 
56     cursor = conn.cursor()  # 获取一个光标
57     id = 1;
58     for dict in provinae_list:
59         sql = 'insert into yiqing (pronvice,total_confirm,total_suspect,total_heal,total_dead,today_confirm,today_suspect,today_heal,today_dead,id,date) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);'
60         pronvice=dict['name']
61         total_confirm=dict['total']['confirm']
62         total_suspect=dict['total']['suspect']
63         total_heal=dict['total']['heal']
64         total_dead=dict['total']['dead']
65         today_confirm=dict['today']['confirm']
66         today_suspect=dict['today']['suspect']
67         today_heal=dict['today']['heal']
68         today_dead=dict['today']['dead']
69         date=json_str['lastUpdateTime']
70         id=id+1
71         sys.stdout.write( dict['name'] + '  ')
72         cursor.execute(sql, [pronvice, total_confirm, total_suspect,total_heal,total_dead,today_confirm,today_suspect,today_heal,today_dead,id,date])
73     print()
74 
75     conn.commit()
76 
77 
78     cursor.close()
79     conn.close()

可视化的代码是根据上次课的代码改了表,改了bean;做起来就相对简单一些

<%@page import="servlet.yqServlet"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@page import="java.util.ArrayList"%>
    <%@page import="bean.yq"%>
    <%@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 rel="stylesheet" href="js/bootstrap.min.css">
<script src="js/jquery-1.8.3.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/echarts.min.js"></script>
<jsp:useBean id="b" class="servlet.yqServlet"></jsp:useBean>
<script type="text/javascript">
function onload() {
    <% 
    
    List<yq> yq = null;
    yq = b.findAll();
    %>
}
</script>
</head>


<body>
<!-- <div style="text-align: center;">
时间:<input type="text" name="Date" >
<a href="1.jsp">查询</a>
</div> -->
<div id="main" style="width:1000px; height: 600px;"></div>

<div align="center">
        <h1 >全国各省疫情信息</h1>
        <table >
            <tr>
                <td>省份</td>
                <td>累计确诊</td>
                <td>累计疑似</td>
                <td>累计治愈</td>
                <td>累计死亡</td>
                <td>今天新增确诊</td>
                <td>今天新增疑似</td>
                <td>今天新增治愈</td>
                <td>今天新增死亡</td>
                
            </tr>
            <c:forEach items="<%=yq %>" var="item">
                <tr>
                    <td>${item.getPronvice()}</td>
                    <td>${item.getTotal_confirm()}</td>
                    <td>${item.getTotal_suspect()}</td>
                    <td>${item.getTotal_heal()}</td>
                    <td>${item.getTotal_dead()}</td>
                    <td>${item.getToday_confirm()}</td>
                    <td>${item.getToday_suspect()}</td>
                    <td>${item.getToday_heal()}</td>
                    <td>${item.getToday_dead()}</td>
                </tr>
            </c:forEach>
        </table>
    </div>
</body>
<script type="text/javascript">

        // 基于准备好的dom,初始化echarts实例
        var myChart = echarts.init(document.getElementById('main'));

        // 指定图表的配置项和数据
        myChart.setOption({
            title: { 
                text: '全国各省确诊人数'
            },
            tooltip: {},
            legend: {
                data:['确诊人数'],
                width:'auto',
                height:'auto'
            },
            xAxis: {
                data: []
            },
            yAxis: {},
            series: [{
                name: '确诊人数',
                type: 'bar',
                data: []
            }]
        });
        myChart.showLoading();
        var names=[];    //类别数组(实际用来盛放X轴坐标值)
        var nums=[];    //销量数组(实际用来盛放Y坐标值)
        // 使用刚指定的配置项和数据显示图表。
        $.ajax({
        type : "post",
        async : true,            //异步请求(同步请求将会锁住浏览器,用户其他操作必须等待请求完成才可以执行)
        url : "s",    //请求发送到TestServlet处
        success : function(resultJson) {
            var result= jQuery.parseJSON(resultJson);
            //请求成功时执行该函数内容,result即为服务器返回的json对象
            if (result) {
                for(var i=0;i<result.length;i++){
                      names.push(result[i].name);    //挨个取出类别并填入类别数组
                      nums.push(result[i].value);
                    }
                   myChart.hideLoading();    //隐藏加载动画
                   myChart.setOption({        //加载数据图表
                       xAxis: {
                           data: names
                       },
                       series: [{
                           // 根据名字对应到相应的系列
                           name: '确诊人数',
                           data: nums
                       }]
                   });
                   
            }
       },
        error : function(errorMsg) {
            //请求失败时执行该函数
        alert("图表请求数据失败!");
        myChart.hideLoading();
        }
   });
    </script>
</html>
newfile.jsp
package bean;
       date类
public class date {
private String name;
private String value;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getValue() {
    return value;
}
public void setValue(String value) {
    this.value = value;
}
}




package bean;

    yq类

public class yq {

    private String pronvice;
    private String total_confirm;
    private String total_suspect;
    private String total_heal;
    private String total_dead;
    
    private String today_confirm;
    private String today_suspect;
    private String today_heal;
    private String today_dead;
    public yq() {
        super();
        // TODO Auto-generated constructor stub
    }
    public yq(String pronvice, String total_confirm, String total_suspect, String total_heal, String total_dead,
            String today_confirm, String today_suspect, String today_heal, String today_dead) {
        super();
        this.pronvice = pronvice;
        this.total_confirm = total_confirm;
        this.total_suspect = total_suspect;
        this.total_heal = total_heal;
        this.total_dead = total_dead;
        this.today_confirm = today_confirm;
        this.today_suspect = today_suspect;
        this.today_heal = today_heal;
        this.today_dead = today_dead;
    }
    public String getPronvice() {
        return pronvice;
    }
    public void setPronvice(String pronvice) {
        this.pronvice = pronvice;
    }
    public String getTotal_confirm() {
        return total_confirm;
    }
    public void setTotal_confirm(String total_confirm) {
        this.total_confirm = total_confirm;
    }
    public String getTotal_suspect() {
        return total_suspect;
    }
    public void setTotal_suspect(String total_suspect) {
        this.total_suspect = total_suspect;
    }
    public String getTotal_heal() {
        return total_heal;
    }
    public void setTotal_heal(String total_heal) {
        this.total_heal = total_heal;
    }
    public String getTotal_dead() {
        return total_dead;
    }
    public void setTotal_dead(String total_dead) {
        this.total_dead = total_dead;
    }
    public String getToday_confirm() {
        return today_confirm;
    }
    public void setToday_confirm(String today_confirm) {
        this.today_confirm = today_confirm;
    }
    public String getToday_suspect() {
        return today_suspect;
    }
    public void setToday_suspect(String today_suspect) {
        this.today_suspect = today_suspect;
    }
    public String getToday_heal() {
        return today_heal;
    }
    public void setToday_heal(String today_heal) {
        this.today_heal = today_heal;
    }
    public String getToday_dead() {
        return today_dead;
    }
    public void setToday_dead(String today_dead) {
        this.today_dead = today_dead;
    }
    @Override
    public String toString() {
        return "yq [pronvice=" + pronvice + ", total_confirm=" + total_confirm + ", total_suspect=" + total_suspect
                + ", total_heal=" + total_heal + ", total_dead=" + total_dead + ", today_confirm=" + today_confirm
                + ", today_suspect=" + today_suspect + ", today_heal=" + today_heal + ", today_dead=" + today_dead
                + "]";
    }
    

}
bean
package servlet;

import java.io.IOException;
import java.util.ArrayList;
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.yq;
import bean.date;
import util.DBUtil;


@WebServlet("/s")
public class s extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
         request.setCharacterEncoding("utf-8"); 
         response.setContentType("text/html;charset=utf-8");
         List<yq> Y = new ArrayList<yq>();
         yqServlet b = new yqServlet();
         Y = b.findAll();
         List<date> date = new ArrayList<date>();
         for(yq y:Y){
             date d = new date();
             d.setName(y.getPronvice());
             d.setValue(y.getTotal_confirm());
             date.add(d);
         }
            Gson gson = new Gson();
            String json = gson.toJson(date);
            System.out.println(json);
            response.getWriter().write(json);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
servlet
package servlet;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sun.org.apache.bcel.internal.generic.Select;

import bean.yq;
import jdk.nashorn.internal.objects.annotations.Where;
import bean.yq;

@SuppressWarnings("unused")
public class yqServlet {
    private Connection conn;
    private PreparedStatement pstmt;
    @SuppressWarnings("static-access")
    public yqServlet()
    {
        conn=new util.DBUtil().getConn();
    }
    public List<yq> findAll()
    {
        try {  
            String sql = "select * from yiqing where id < 36";
            pstmt=conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            List<yq> Y = new ArrayList<yq>();
            while(rs.next()) 
            {
                yq user=new yq();
                user.setPronvice(rs.getString(1));
                user.setTotal_confirm(rs.getString(2));
                user.setTotal_suspect(rs.getString(3));
                user.setTotal_dead(rs.getString(4));
                user.setTotal_heal(rs.getString(5));;
                user.setToday_confirm(rs.getString(6));;
                user.setToday_heal(rs.getString(7));;
                user.setToday_suspect(rs.getString(8));
                user.setToday_dead(rs.getString(9));
                Y.add(user);
            }
            return Y;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
dao

 

posted @ 2020-03-14 15:44  大米粒o  阅读(187)  评论(0编辑  收藏  举报