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. onbeforeunload事件兼容性操作

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  2. Java Random函数

    Java中存在着两种Random函数: 1.java.lang.Math.Random: 调用这个Math.Random()函数能够返回带正号的double值,该值大于等于0.0且小于1.0,即取值范 ...

  3. –IDEA+Maven+JavaWeb+tomcat项目搭建(图文并茂,详细)

    一.创建Maven项目 1:如果刚打开IDEA,显示的是这个页面,我们直接单击 Create New Project(创建项目) 或者 File-> New-> Project 2:选中左 ...

  4. hexo博客的学习笔记

    这篇文章主要的作用是作为 .md文件打开,内部的格式为一个初学者对hexo以及markdown语法运用的笔记 1.Hexo的写文格式 最开始为文章的属性部分,以三横杠-开始,-结束.里面记录了文章的标 ...

  5. Ubuntu环境下的iptables的端口转发配置实例

    打开转发开关要让iptables的端口转发生效,首先需要打开转发开关方法一:临时打开,重启后失效$sudo su#echo 1 >/proc/sys/net/ipv4/ip_forward 方法 ...

  6. 【h5ai】搭建服务器目录

    在前几天,我帮人安装h5ai这个东西,结果直接踩坑,装了一个下午,打算自己也装一个,顺便写一下教程 最终效果演示: https://larsjung.de/h5ai/demo/ 服务器 服务器这里推荐 ...

  7. Java入门教程十一(异常处理)

    在程序设计和运行的过程中,发生错误是不可避免的.尽管 Java 语言的设计从根本上提供了便于写出整洁.安全代码的方法,并且程序员也尽量地减少错误的产生,但是使程序被迫停止的错误的存在仍然不可避免.为此 ...

  8. Docker Compose 项目打包部署

    Docker Compose 前面我们使用 Docker 的时候,定义 Dockerfile 文件,然后使用 docker build.docker run 等命令操作容器.然而微服务架构的应用系统一 ...

  9. HTML5前期学习准备(一)

    HTML简介 1.html的基本概念 HTML:HyperTextMarket language,超文本标记语言(所谓"超文本"就是指页面内可以包含图片.链接,甚至音乐.程序等非文 ...

  10. scroll-view组件bindscroll实例应用:自定义滚动条

    我们知道scroll-view组件作为滑动控件非常好用,而有时候我们想放置一个跟随滚动位置来跟进的滚动条,但又不想用滚动条api该怎么办呢?(当然是自己写一个呗还能怎么办[自黑冷漠脸])嗯,没错.自己 ...