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. 亚马逊Prime会员的杀价,能说明会员+会越来越便宜吗?

    前段时间,京东又坑了!京东调整了物流方案--从原来的购物不满49元只需6元运费,调整到购物不满46元运费15元,运费猛涨了9元!原本京东PLUS会员每月有5张免运费券,但在运费涨价后运费券限制在6元, ...

  2. H5 video 标签 详解

    昨天使用H5  video 标签 写了视频播放   本打算参考爱奇艺的代码进行修改  发现 它是动态数据  静态页面需要拆解代码 我情急之下  使用了  video   整理一下笔记   后面有人用 ...

  3. 吴裕雄--天生自然 python数据分析:葡萄酒分析

    # import pandas import pandas as pd # creating a DataFrame pd.DataFrame({'Yes': [50, 31], 'No': [101 ...

  4. Dungeon Master (三维BFS)

    题目: You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed o ...

  5. JAVA SE Lesson 1

    1.  类是一种抽象的概念,对象是类的一种具体表示形式,是具体的概念.先有类,然后由类来生成对象(Object).对象又叫做实例(Instance).2.  类由两大部分构成:属性以及方法.属性一般用 ...

  6. RHEL系统下安装atlassian-jira-5

    操作系统:RHEL 6.4 x86_64 Jira版本:atlassian-jira-5.2.11-x64.bin 安装路径:/opt/atlassian/jira/ 数据保存路径:/opt/atla ...

  7. grpc调试工具

    grpcurl 和 grpcui 都是调试grpc的利器,前者用于命令行,类似curl工具:后者是以web的形式进行调试的,类似postman工具. 有了这两款工具,我们不用写任何客户端代码,也能方便 ...

  8. Linux 环境 搭建Git 服务器,并且修改SSH端口使用

    1.环境配置说明 服务器 CentOS 7 + git(git version 1.8.3.1) 客户端 Windows10 + SourceTree 2.安装 Git 服务器端安装: sudo yu ...

  9. 第一章 感受mac之美-换一种方式用电脑,开启新历程

    感谢关注我的读者一直以来的追随与信任.去年到今年以来大环境都不是很好.裁员,机构优化,工厂倒闭,公司破产,贸易战等消息传来,不少还是身边发生的.今年开年以来更是有病毒横行,天降蝗灾等灾害.愿大家都好好 ...

  10. http协议、加密解密、web安全

    今天,就简单讲讲,我学习的知识.http协议:http协议是超文本传输协议,是用于传输超媒文档的应用层协议,同时,http协议是无状态协议,意味着,在服务器两个请求之间不会保留任何数据.虽然通常基于T ...