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爬取丁香医生疫情数据并存储至数据库的更多相关文章

  1. 利用Jsoup爬取新冠疫情数据并存至数据库

    需要用到的jar包(用来爬取的jsoup,htmlunit-2.37.0-bin以及连接数据库中的mysql.jar) 链接:https://pan.baidu.com/s/1VlylWmlhjd8K ...

  2. Python:爬取全国各省疫情数据并在地图显示

    代码: import requests import pymysql import json from pyecharts import options as opts from pyecharts. ...

  3. Python:爬取中国各市的疫情数据并存储到数据库

    import requests import pymysql import json def create(): # 连接数据库 db = pymysql.connect(host = 'localh ...

  4. 5分钟python爬虫案例,手把手教爬取国内外最新疫情历史数据

    俗话说的好,“授之以鱼不如授之以渔”,所以小编今天就把爬疫情历史数据的方法分享给你们. 基本思路:分析腾讯新闻“抗肺炎”版块,采用“倒推法”找到疫情数据接口,然后用python模拟请求,进而保存疫情历 ...

  5. Java爬取B站弹幕 —— Python云图Wordcloud生成弹幕词云

    一 . Java爬取B站弹幕 弹幕的存储位置 如何通过B站视频AV号找到弹幕对应的xml文件号 首先爬取视频网页,将对应视频网页源码获得 就可以找到该视频的av号aid=8678034 还有弹幕序号, ...

  6. Java爬取同花顺股票数据(附源码)

    最近有小伙伴问我能不能抓取同花顺的数据,最近股票行情还不错,想把数据抓下来自己分析分析.我大A股,大家都知道的,一个概念火了,相应的股票就都大涨. 如果能及时获取股票涨跌信息,那就能在刚开始火起来的时 ...

  7. Java 爬取 51job 数据 WebMagic实现

    Java 爬取 51job 数据 一.项目Maven环境配置 相关依赖 jar 包配置 <parent> <groupId>org.springframework.boot&l ...

  8. Java爬取校内论坛新帖

    Java爬取校内论坛新帖 为了保持消息灵通,博主没事会上上校内论坛看看新帖,作为爬虫爱好者,博主萌生了写个爬虫自动下载的想法. 嗯,这次就选Java. 第三方库准备 Jsoup Jsoup是一款比较好 ...

  9. Java爬取网易云音乐民谣并导入Excel分析

    前言 考虑到这里有很多人没有接触过Java网络爬虫,所以我会从很基础的Jsoup分析HttpClient获取的网页讲起.了解这些东西可以直接看后面的"正式进入案例",跳过前面这些基 ...

随机推荐

  1. Python---14面向对象高级编程(__slots__&@property)

    一.使用__slots__ 正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性.先定义class: class Stude ...

  2. 携程酒店DevOps测试实践

    作者简介 王幸福,携程酒店研发部高级测试经理,负责无线自动化测试相关工作.在测试框架和平台研发.移动测试.DevOps等领域有着丰富的经验. 如今很多大型互联网公司.创新型企业都在积极地进行DevOp ...

  3. Protobuf 简介及简单应用

    Protobuf 是 protocol buffers 的缩写. 根据官网的说法, protocol buffers 与平台无关, 与语言无关, 实现数据序列化的一种手段. 正如名字一样, proto ...

  4. android activity 启动过程分析(source code 4.4)

    说实话,android source code从2.3到4.4变化是蛮多的,尤其是media部分,虽然总的框架是没有多大变化,但是找起代码来看还是挺麻烦的.在android里面最受伤的是使用了java ...

  5. 从游戏到汽车 “明星IP”的发财轨迹

    "明星IP"的发财轨迹" title="从游戏到汽车 "明星IP"的发财轨迹"> 移动互联网时代的开启,不仅彻底重构了大众生 ...

  6. USB小白学习之路(4)HID键盘程序

    HID键盘程序 1. 特别注意 需要特别注意,各个例程中的设备描述符,配置描述符等各种描述符都是已经配置好了的,我们需要做的只是在例程中将代码修改为自己需要的部分即可,一般情况下是不可以串搭配的. 2 ...

  7. Ubuntu 16.04 apt 国内源

    一.推荐几个 Ubuntu 16.04 国内的 apt 源 1. 阿里源 # deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (2 ...

  8. java单链表的实现自己动手写一个单链表

    单链表:单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素.链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是 ...

  9. 精简TTF字体、FontPruner、汉字字体瘦身(非字蛛)

    20190726更新 标黄部分 网上比应用比较多的 字蛛 http://font-spider.org/ 本文使用了本机安装软件,得到瘦身后的 TTF 字体文件 准备工具: python : 我使用是 ...

  10. OpenCV图像增强(python)

    为了得到更加清晰的图像我们需要通过技术对图像进行处理,比如使用对比度增强的方法来处理图像,对比度增强就是对图像输出的灰度级放大到指定的程度,获得图像质量的提升.本文主要通过代码的方式,通过OpenCV ...