数据分析疫情图——day8

数据分析疫情图——day8


前言

好好学习,javaweb制作全国疫情展示,接下来要用java爬虫爬取数据,然后放到数据库,以下是今天学习的知识(身体不舒服简单整理一下),

第一天:数据分析疫情图——day1
第二天:数据分析疫情图——day2
第三天:数据分析疫情图——day3
第四天:数据分析疫情图——day4
第五天:数据分析疫情图——day5
第六天:数据分析疫情图——day6
第七天:数据分析疫情图——day7

在这里插入图片描述

一.获取数据,数据解析

用到的包

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.sound.midi.MidiDevice.Info;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.Test;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.tedu.entity.*;

自定义类

public class Province {
	//成员变量
	private String provinceName;
	private int currentConfirmedCount;
	private int confirmedCount;
	private int deadCount;
	private int curedCount;
	//getter/setter方法
	
	public String getProvinceName() {
		return provinceName;
	}

	public void setProvinceName(String provinceName) {
		this.provinceName = provinceName;
	}

	public int getCurrentConfirmedCount() {
		return currentConfirmedCount;
	}

	public void setCurrentConfirmedCount(int i) {
		this.currentConfirmedCount = i;
	}

	public int getConfirmedCount() {
		return confirmedCount;
	}

	public void setConfirmedCount(int i) {
		this.confirmedCount = i;
	}

	public int getDeadCount() {
		return deadCount;
	}

	public void setDeadCount(int deadCount) {
		this.deadCount = deadCount;
	}

	public int getCuredCount() {
		return curedCount;
	}

	public void setCuredCount(int curedCount) {
		this.curedCount = curedCount;
	}

	@Override
	public String toString() {
		return "Province [provinceName=" + provinceName + ", currentConfirmedCount=" + currentConfirmedCount
				+ ", confirmedCount=" + confirmedCount + ", deadCount=" + deadCount + ", curedCount=" + curedCount
				+ "]";
	}
	

}

@Test
	public void test01() {
		//1、要爬取的网站
		String url = "https://ncov.dxy.cn/ncovh5/view/pneumonia";
		//2、爬取出整个页面
		Document doc = null;
		try {
			doc = Jsoup.connect(url)
				 .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36")
				 .get();
			//System.out.println(doc);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String resultStr = doc.html();
		//System.out.println(result);
		
		
		//3、做数据的筛选
		String regex = "window.getAreaStat = (.*?)\\}(catch)";
		Pattern patt = Pattern.compile(regex);
		System.out.println(patt);
		Matcher matcher = patt.matcher(resultStr);
		//System.out.println(matcher);
		String result = "";
		if (matcher.find()) {
			result = matcher.group(1);
			//System.out.println(result);
			
			
			//数据的解析----------json数据
			//导入jar包   Jackson:  fastjson:alibaba
			ObjectMapper mapper =new ObjectMapper();
			try {
				JsonNode jn= mapper.readTree(result);
				Province[] s=new Province[jn.size()+1];
				
				
				//System.out.println(jn);
			for (int i = 0; i < jn.size(); i++) {
				s[i]=new Province();
				s[i].setProvinceName(jn.get(i).get("provinceName").asText());
				s[i].setCurrentConfirmedCount(jn.get(i).get("currentConfirmedCount").asInt());
				s[i].setConfirmedCount(jn.get(i).get("confirmedCount").asInt());
				s[i].setDeadCount(jn.get(i).get("deadCount").asInt());
				s[i].setCuredCount(jn.get(i).get("curedCount").asInt());

				System.out.println(s[i].toString());
			}
			} catch (IOException e) {
				// TODO: handle exception
			}
			
		}
		

	

在这里插入图片描述

二.数据库连接

你要事先创建好数据库da01,然后在创建province数据表

在这里插入图片描述
导入的包

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
	public static void main(String[] args) throws Exception {
		//1.实现数据库的驱动
		Class.forName("com.mysql.jdbc.Driver");
		String url = "jdbc:mysql://localhost:3300/da01";
		String user = "root";//数据库的用户名
		String password = "root";//数据库的密码
		//2.获取数据库的连接
		Connection conn = DriverManager.getConnection(url, user, password);
		System.out.println(conn);//com.mysql.jdbc.JDBC4Connection@21b8d17c
		//3、获取传输器对象
		Statement stat = conn.createStatement();
		//发送SQL
		String sql = "insert into province values('陈',123,156,23,6)";
		int rows = stat.executeUpdate(sql);
		System.out.println("影响了" + rows + "行");
	}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 博客之星2020 设计师:CY__ 返回首页