Java爬取丁香医生疫情数据并存储至数据库
1、通过页面的url获取html代码
// 根URL
private static String httpRequset(String requesturl) throws IOException {
StringBuffer buffer = null;
BufferedReader bufferedReader = null;
InputStreamReader inputStreamReader = null;
InputStream inputStream = null;
HttpsURLConnection httpsURLConnection = null;
try {
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);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return buffer.toString();
}
2、获取省市疫情数据
/**
* 获取全国各个省市的确诊、死亡和治愈人数
*
* @return
*/
public static String getAreaStat() {
String url = "https://ncov.dxy.cn/ncovh5/view/pneumonia";
String htmlResult = "";
try {
htmlResult = httpRequset(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println(htmlResult); // 正则获取数据
// 因为html的数据格式看着就像json格式,所以我们正则获取json
String reg = "window.getAreaStat = (.*?)\\}(?=catch)";
Pattern totalPattern = Pattern.compile(reg);
Matcher totalMatcher = totalPattern.matcher(htmlResult); String result = "";
if (totalMatcher.find()) {
result = totalMatcher.group(1);
System.out.println(result);
// 各个省市的是一个列表List,如果想保存到数据库中,要遍历结果,下面是demo
JSONArray array = JSONArray.parseArray(result);
try {
Connection con =BaseConnection.getConnection("VData");
Statement stmt = con.createStatement();
Date date=new Date(System.currentTimeMillis());
for (int i = 0; i <= 30; i++) { com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject
.parseObject(array.getString(i));
String provinceName = jsonObject.getString("provinceName");
String current = jsonObject.getString("currentConfirmedCount");
String confirmed = jsonObject.getString("confirmedCount");
String cured = jsonObject.getString("curedCount");
String dead = jsonObject.getString("deadCount");
String suspect=jsonObject.getString("suspectedCount");
stmt.executeUpdate("insert into province values('"+provinceName+"','"+confirmed+"','"+suspect+"','"+cured+
"','"+dead+"','"+current+"','"+date+"')"); JSONArray array2 = jsonObject.getJSONArray("cities");
for (int j = 0; j < array2.size(); j++) {
com.alibaba.fastjson.JSONObject jsonObject2 = com.alibaba.fastjson.JSONObject
.parseObject(array2.getString(j));
String cityname = jsonObject2.getString("cityName");
String current2 = jsonObject2.getString("currentConfirmedCount");
String confirmed2 = jsonObject2.getString("confirmedCount");
String cured2 = jsonObject2.getString("curedCount");
String dead2 = jsonObject2.getString("deadCount");
String suspect2 = jsonObject2.getString("suspectedCount");
System.out.println();
stmt.executeUpdate("insert into city values('"+cityname+"','"+confirmed2+"','"+suspect2+"','"+cured2+"','"+dead2+"','"+current2+"','"+provinceName+"','"+date+"')");
}
}
stmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}
3、获取世界疫情数据
/**
* 世界
*
* @return
*/
public static String getCountryData() {
String url = "https://ncov.dxy.cn/ncovh5/view/pneumonia";
String htmlResult = "";
try {
htmlResult = httpRequset(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String reg = "window.getListByCountryTypeService2true = (.*?)\\}(?=catch)";
Pattern totalPattern = Pattern.compile(reg);
Matcher totalMatcher = totalPattern.matcher(htmlResult); String result = "";
if (totalMatcher.find()) {
result = totalMatcher.group(1);
System.out.println(result);
JSONArray array = JSONArray.parseArray(result);
try {
Connection con =BaseConnection.getConnection("VData");
Statement stmt = con.createStatement();
for(int i=0;i<array.size();i++) {
com.alibaba.fastjson.JSONObject jsobj=com.alibaba.fastjson.JSONObject.parseObject(array.getString(i));
if(!jsobj.getString("provinceName").equals("中国")) {
Date date = new Date(Long.parseLong(jsobj.getString("createTime")));
String s="insert into contury values('"+jsobj.getString("continents")+"','"+jsobj.getString("provinceName")
+"','"+jsobj.getString("currentConfirmedCount")+"','"+jsobj.getString("confirmedCount")+"','"+jsobj.getString("suspectedCount")
+"','"+jsobj.getString("curedCount")+"','"+jsobj.getString("deadCount")+"','"+date+"')";
stmt.executeUpdate(s);
}
}
stmt.close();
con.close();
}catch (Exception e) {
// TODO: handle exception
}
}
return "";
}
Java爬取丁香医生疫情数据并存储至数据库的更多相关文章
- 利用Jsoup爬取新冠疫情数据并存至数据库
需要用到的jar包(用来爬取的jsoup,htmlunit-2.37.0-bin以及连接数据库中的mysql.jar) 链接:https://pan.baidu.com/s/1VlylWmlhjd8K ...
- Python:爬取全国各省疫情数据并在地图显示
代码: import requests import pymysql import json from pyecharts import options as opts from pyecharts. ...
- Python:爬取中国各市的疫情数据并存储到数据库
import requests import pymysql import json def create(): # 连接数据库 db = pymysql.connect(host = 'localh ...
- 5分钟python爬虫案例,手把手教爬取国内外最新疫情历史数据
俗话说的好,“授之以鱼不如授之以渔”,所以小编今天就把爬疫情历史数据的方法分享给你们. 基本思路:分析腾讯新闻“抗肺炎”版块,采用“倒推法”找到疫情数据接口,然后用python模拟请求,进而保存疫情历 ...
- Java爬取B站弹幕 —— Python云图Wordcloud生成弹幕词云
一 . Java爬取B站弹幕 弹幕的存储位置 如何通过B站视频AV号找到弹幕对应的xml文件号 首先爬取视频网页,将对应视频网页源码获得 就可以找到该视频的av号aid=8678034 还有弹幕序号, ...
- Java爬取同花顺股票数据(附源码)
最近有小伙伴问我能不能抓取同花顺的数据,最近股票行情还不错,想把数据抓下来自己分析分析.我大A股,大家都知道的,一个概念火了,相应的股票就都大涨. 如果能及时获取股票涨跌信息,那就能在刚开始火起来的时 ...
- Java 爬取 51job 数据 WebMagic实现
Java 爬取 51job 数据 一.项目Maven环境配置 相关依赖 jar 包配置 <parent> <groupId>org.springframework.boot&l ...
- Java爬取校内论坛新帖
Java爬取校内论坛新帖 为了保持消息灵通,博主没事会上上校内论坛看看新帖,作为爬虫爱好者,博主萌生了写个爬虫自动下载的想法. 嗯,这次就选Java. 第三方库准备 Jsoup Jsoup是一款比较好 ...
- Java爬取网易云音乐民谣并导入Excel分析
前言 考虑到这里有很多人没有接触过Java网络爬虫,所以我会从很基础的Jsoup分析HttpClient获取的网页讲起.了解这些东西可以直接看后面的"正式进入案例",跳过前面这些基 ...
随机推荐
- python jQuery筛选器
筛选器:$(this).next() 下一个 $(this).prev 上一个 $(this).parent() 父 $(this).children() 孩 $(th ...
- python 有关堡垒机的那些事
堡垒机为了保证系统或服务器的安全性,防止运维和开发人员胡乱操作服务器,导致不必要的损失,使用堡垒机来完成对运维和开发人员的授权.用户统一登录堡垒机账号来操作系统或服务器.堡垒机等于成了生产系统的SSO ...
- 旅游机票类专业名词---PNR
PNR: PNR是旅客订座记录,即Passenger Name Record的缩写,它反映了旅客的航程,航班座位占用的数量,及旅客信息.适用民航订座系统. 一个PNR由以下几项组成: 姓名组NM 航段 ...
- xstream的介绍及用法
使用xstream工具包导入xpp3_min-1.1.4c和xstream-1.4.9特点:代码简洁,超级方便,可以自己定义xml格式(适合做文件传输)属性特点:1. xStream.alias(&q ...
- 从摔得稀碎、蓝屏再到黄牛拒绝加价:iPhone X究竟是怎么了
X究竟是怎么了" title="从摔得稀碎.蓝屏再到黄牛拒绝加价:iPhone X究竟是怎么了"> 近日,iPhone X终于迎来了正式出货的时间.作为十周年的创 ...
- u-boot的环境变量详解
u-boot的环境变量 u-boot的环境变量是使用u-boot的关键,它可以由你自己定义的,但是其中有一些也是大家经常使用,约定熟成的,有一些是u-boot自己定义的,更改这些名字会出现错 ...
- kafka相关问题总结
一直在使用kafka,遇到过很多问题,总结一下 很多人对比kafka和AMQP的时候,都会强调kafka会丢数据,感觉好像只要用kafka就会丢数据一样,从而排斥使用kafka,亦或者在使用的过程中, ...
- 【RxJava Demo分析】(二)Schedulers线程调度器 · Hans Zone
用Schedulers(调度器)实现多任务(并发,Concurrency)的例子 废话不多说我们看一下有关于RxJava的代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- Spark基础全解析
我的个人博客:https://www.luozhiyun.com/ 为什么需要Spark? MapReduce的缺陷 第一,MapReduce模型的抽象层次低,大量的底层逻辑都需要开发者手工完成. 第 ...
- Node REPL环境
1.概述 REPL全称Read,Eval,Print,Loop,简单理解为接收用户输入,执行用户输入,打印执行结果并输出到控制台,进行下一次轮回,可以进行一些简单的测试,类似于浏览器的控制台. 命令行 ...