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获取的网页讲起.了解这些东西可以直接看后面的"正式进入案例",跳过前面这些基 ...
随机推荐
- C++走向远洋——28(项目三,时间类,2)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:time.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- C++扬帆远航——19(斐波那契数列第20项)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:fib.cpp * 作者:常轩 * 微信公众号:Worldhel ...
- 自己动手用java写一个hashMap
入坑java很多年了,现在总结一下自己学到的东西. 1.首先我们先来聊聊什么是HashMap? 什么是hash?hash用中文的说法就叫做“散列”,通俗的讲就是把任意长度的字符串输入,经过hash计算 ...
- PHPRAP v1.0.6 发布,修复因php7.1版本遗弃mcrypt扩展造成安装失败的BUG
PHPRAP,是一个PHP轻量级开源API接口文档管理系统,致力于减少前后端沟通成本,提高团队协作开发效率,打造PHP版的RAP. 更新记录 [修复]修复因php7.1版本遗弃mcrypt扩展造成安装 ...
- XML学习笔记--背诵版
前言 一直想系统性的学XML,就没时间学,今晚抽出几个小时时间学完了XML.过几天再过来看看,背一背应该就差不多,记得东西较多,没什么难理解的. XML数据传输格式 第一章 XML概述 1.1 引入 ...
- DOM3中的自定义事件
DOM3级还定义了自定义事件,自定义事件不是由DOM原生触发的,它的目的是让开发人员创建自己的事件.要创建的自定义事件可以由createEvent("CustomEvent"); ...
- sqlserver取分组数据的最后一条数据
SQL Server中ROW_NUMBER()函数的使用 参考文章:https://blog.csdn.net/pan_junbiao/article/details/79941162 业务中的问题: ...
- Python-Pyquery库的安装和调用
解析库pyquery:# pip安装pyquery库pip3 install pyquery from pyquery import PyQuery as pq # 定义doc,输入html源代码 d ...
- 【推荐算法工程师技术栈系列】分布式&数据库--tensorflow
目录 TensorFlow 高阶API Dataset(tf.data) Estimator(tf.estimator) FeatureColumns(tf.feature_column) tf.nn ...
- volatile关键字的理解
volatile是java语言提供的一种稍弱的同步机制,它的作用是能够保证被volatile修饰的变量,每个线程在获取它的值时都能获取到最新的值. 要理解这个原理首先要知道java内存模型:每个线程都 ...