实战爬取疫情信息
目标网站
https://ncov.dxy.cn/ncovh5/view/pneumonia
导包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.2</version>
</dependency>
分析HTML,找出数据部分
<script id="getListByCountryTypeService2true">try { window.getListByCountryTypeService2true = [{"id":3307655,"createTime":1589418316000,"modifyTime":1589418316000,"tags":"","countryType":2,"continents":"北美洲","provinceId":"8","provinceName":"美国","provinceShortName":"","cityName":"","currentConfirmedCount":1061715,"confirmedCount":1388936,"confirmedCountRank":1,"suspectedCount":0,"curedCount":243430,"deadCount":83791,"deadCountRank":1,"deadRate":"6.03","deadRateRank":32,"comment":"","sort":0,"operator":"hejiashu","locationId":971002,"countryShortCode":"USA","countryFullName":"United States of America","statisticsData":"https://file1.dxycdn.com/2020/0315/553/3402160512808052518-135.json","incrVo":{"currentConfirmedIncr":0,"confirmedIncr":0,"curedIncr":0,"deadIncr":0},"showRank":true},{"id":3307761,"createTime":1589418316000,"modifyTime":1589418316000,"tags":"","countryType":2,"continents":"欧洲","provinceId":"10","provinceName":"英//img1.dxycdn.com/2020/0324/775/3403801407211095604-135.png","title":"治愈"},{"imgUrl":"https://img1.dxycdn.com/2020/0324/250/3403801424390727199-135.png","title":"死亡"}],"hbFeiHbTrendChart":[{"imgUrl":"https://img1.dxycdn.com/2020/0326/624/3404177508906904585-135.png","title":"非湖北新增确诊"},{"imgUrl":"https://img1.dxycdn.com/2020/0326/777/3404177532529485065-135.png","title":"湖北新增确诊"},{"imgUrl":"https://img1.dxycdn.com
······
{"currentConfirmedCount":2495457,"confirmedCount":4214734,"suspectedCount":4,"curedCount":1428157,"deadCount":291120,"suspectedIncr":0,"currentConfirmedIncr":0,"confirmedIncr":0,"curedIncr":0,"deadIncr":0},"globalStatistics":{"currentConfirmedCount":2495656,"confirmedCount":4299198,"curedCount":1507778,"deadCount":295764,"currentConfirmedIncr":-21,"confirmedIncr":6,"curedIncr":27,"deadIncr":0},"globalOtherTrendChartData":"https://file1.dxycdn.com/2020/0514/115/3413256977429064839-135.json"}}catch(e){}</script><script id="fetchWHOArticle">try { window.fetchWHOArticle = {"id":1,"title":"新冠病毒会变异?口罩不够怎么办?世界卫生组织的答疑来了!","linkUrl":"https://mp.weixin.qq.com/s/6q0qMFXzoKI7MMvY7zrXUw","imgUrl":"https://img1.dxycdn.com/2020/0220/196/3397701576545475720-135.jpg","operator":"liz","showable":true,"createTime":1582115584000,"modifyTime":1582180971000}}catch(e){}</script><script id="fetchGoodsGuide">try { window.fetchGoodsGuide = [{"id":1,"categoryName":"消毒剂","title":"消毒剂指南","recordStatus":1,"contentImgUrls":["https://img1.dxycdn.com/2020/0215/220/3396780175063930893-135.png","https://img1.dxycdn.com/2020/0215/637/3396780181506594738-135.png","https://img1.dxycdn.com/2020/0215/372/3396780187949046019-135.png"],"sort":1},{"id":2,"categoryName":"口罩","title":"口罩指南","recordStatus":1,"contentImgUrls":["https://img1.dxycdn.com/2020/0225/169/3398653034208226202-135.png"],"sort":2},{"id":3,"categoryName":"洗手液","title":"洗手液指南","recordStatus":1,"contentImgUrls":["https://img1.dxycdn.com/2020/0215/703/3396784974690083861-135.png"],"sort":3},{"id":4,"categoryName":"温度计","title":"温度计指南","recordStatus":1,"contentImgUrls":["https://img1.dxycdn.com/2020/0215/743/3396785021934726693-135.png"],"sort":4}]}catch(e){}</script><script id="getIndexRecommendListundefined">try { window.getIndexRecommendListundefined = [{"id":173,"createTime":1585103455000,"modifyTime":1585103455000,"contentType":1,"title":"传染病全球大流行,历史早就告诉我们的 5 件事","imgUrl":"https://img1.dxycdn.com/2020/0325/826/3403983726425257144-135.jpg","linkUrl":"https://mp.weixin.qq.com/s?__biz=MjA1ODMxMDQwMQ==&mid=2657278282&idx=1&sn=ec5a88bf6cead3079f2f48f68f64aa93&chksm=4906dd247e715432a91c3c99e4d92082ffd2eb8c6ddf4355b833f597d9464aa22ad048d84d0b&token=2114569265&lang=zh_CN#rd","recordStatus":1,"sort":300,"operator":"zhengjiaxuan","countryType":1},{"id":155,"createTime":1584084790000,"modifyTime":1584084790000,"contentType":4,"title":"1 个错误动作,严重烧伤!10 个消毒常见错误千万别再犯了","imgUrl":"https://img1.dxycdn.com/2020/0313/012/3401794963846645571-135.jpg","linkUrl":"https://mp.weixin.qq.com/s/Zaku42njWNpljKF3mViaiA","recordStatus":1,"sort":299,"operator":"zhengjiaxuan","countryType":1},{"id":154,"createTime":1583223980000,"modifyTime":1583223980000,"contentType":2,"title":"特殊时期,孩子感冒咳嗽了到底要不要去医院?","imgUrl":"https://img1.dxycdn.com/2020/0303/889/3399947603922974408-135.jpg","linkUrl":"https://mp.weixin.qq.com/s/T9QvVHgkE30lMn_fC3EBBw","recordStatus":1,"sort":298,"operator":"zhengjiaxuan","countryType":1},{"id":152,"createTime":1583223816000,"modifyTime":1583223816000,"contentType":1,"title":"电动口罩有没有用?","imgUrl":"https://img1.dxycdn.com/2020/0303/345/3399947210933784082-135.jpg","linkUrl":"https://mp.weixin.qq.com/s/c98MyhQiJ3aiNWjjseYGbg","recordStatus":1,"sort":297,"operator":"zhengjiaxuan","countryType":1},{"id":153,"createTime":1583223881000,"modifyTime":1583223881000,"contentType":2,"title":"因为疫情耽误的疫苗接种,该如何安排补种?","imgUrl":"https://img1.dxycdn.com/2020/0303/678/3399947393469903332-135.jpg","linkUrl":"https://mp.weixin.qq.com/s/dejV_MZSZSZvlpSliER1dQ","recordStatus":1,"sort":296,"operator":"zhengjiaxuan","countryType":1}]}catch(e){}</script><script id="getIndexRumorList">try { window.getIndexRumorList = [{"id":181,"title":"可在家使用新冠病毒试剂盒自测?","mainSummary":"北京市药监局提示:普通市民家庭不可自行使用","summary":"","body":"近日,有人在朋友圈兜售某公司生产的新冠病毒抗体检测试剂盒,单价 150 元,并宣称可以家庭自行使用。对此,北京市药监局提示,经批准注册的新冠病毒检测试剂盒,均需要具备 PCR 实验室及专用设备的医疗机构才能完成检测,普通市民家庭不可自行使用,市民不要轻信虚假宣传,出现相关症状应及时就医。","sourceUrl":"","score":1000,"rumorType":0},{"id":184,"title":"疫情期间大强度锻炼可提高抵抗力?","mainSummary":"国家体育总局体育科学研究所研究员徐建方:长时间大强度的运动,会导致身体机能失调,免疫功能下降","summary":"","body":"长时间大强度的运动,会导致身体机能失调,免疫功能下降,并且运动损伤风险增加。因此,特别忌讳平常不运动、锻炼搞突击。什么样的运动强度是适宜的:运动后感觉轻度的呼吸急促,周身微热,面色微红,内心感觉轻松愉快。虽然稍微感觉有点疲乏,但是经过休息以后可以消除,并且没有疼痛和麻木。","sourceUrl":"","score":1000,"rumorType":0},{"id":182,"title":"吃降压药会增加感染病毒的风险?","mainSummary":"丁香医生团队辟谣:目前没有任何动物和临床研究数据证实这种说法的可靠性","summary":"","body":"因现有研究显示 ACE2 是新型冠状病毒入侵人体的关键,网传服用 ACEI(血管紧张素转化酶抑制剂)类降压药会增加感染新冠病毒的风险。但目前没有任何动物和临床研究数据证实。擅自停用降压药会导致血压波动,给高血压患者带来更大的危险。对高血压患者而言,所有的药物调整都要在专科医生的指导下进行。","sourceUrl":"","score":197,"rumorType":0},{"id":183,"title":"改用吸痰机后,武汉死亡人数减半?","mainSummary":"刘良教授辟谣:没有干预过任何个例的临床治疗","summary":"","body":"有传闻称:「病患遗体解剖发现死者肺部出现大量痰栓,而痰栓是由呼吸机使用所产生,致人缺氧而死。昨天开始,急救改用吸痰机,帮助武汉死亡人数减半,这需要感谢遗体捐献者和同济法医系刘良教授团队的努力。」对此,刘良教授亲自辟谣:没有干预过任何个例的临床治疗。\n法医学专家表示:「痰栓不是呼吸机导致的,而是由肺部黏液病变形成的,属于疾病损害后的病理过程。」","sourceUrl":"","score":196,"rumorType":0},{"id":180,"title":"第一例新冠肺炎患者来自美国?","mainSummary":"丁香医生团队查证:系翻译错误","summary":"","body":"网传的新闻截图原文「CDC confirms first coronavirus case of \"unknown\"origin in U.S.」的意思其实是:「美国疾控中心确认美国出现首例无法确定病源新冠肺炎患者。」网友说得好,只背单词不学语法的人,就不要瞎翻译了。","sourceUrl":"","score":195,"rumorType":0},{"id":179,"title":"新冠病毒传播途径有变化?","mainSummary":"科技部社会发展科技司司长吴远彬:目前研究结论显示,呼吸道飞沫和密切接触传播仍然是主要传播途径","summary":"","body":"28 日,科技部社会发展科技司司长吴远彬表示,目前研究结论显示,呼吸道飞沫和密切接触传播仍然是主要传播途径。粪口传播有一定风险,但传播能力和条件还需进一步相应研究证实。针对气溶胶传播,中国医科院实验动物所开展相应实验,气溶胶传播要同时满足密闭的空间、较长的时间、高浓度病毒三个条件,在这些条件下才有传染的可能性。通风条件良好的日常生活中气溶胶传播可能性小。","sourceUrl":"","score":193,"rumorType":0},{"id":177,"title":"十三名男女生感染 sk5 病毒?","mainSummary":"丁香医生团队查证:该谣言系 2018 年的旧谣言,造谣者已被查处","summary":"","body":"近日,部分网民转发「乐陵十三人染 sk5 病毒,参与抢救的医生已被隔离」的信息,其实,该谣言早在 2018 年就已经存在,且造谣者被依法查处。此次经乐陵市公安局侦查,确认该谣言在当前时期的首次转发者为董某玲(庆云县居住)。2020 年 2 月 27 日,董某玲因散布谣言扰乱公共秩序,已被公安机关依法处以行政拘留。","sourceUrl":"","score":192,"rumorType":0},{"id":173,"title":"十年前就有人预测疫情?","mainSummary":"上海图书馆查证:没有找到传闻里提到的这本书,同名文章也没有相关内容","summary":"","body":"网传「陈国生写的一本叫《实证化中医基础理论及运用》的书在十年前预言了这次肺炎疫情」。上海图书馆进行了查证,没有检索到这本书。\n在万方数据库里,可以查得陈国生所写《实证化中医基础理论依据及应用》这篇文章,发表在《2011 年全国天灾预测研讨学术会议论文集》中,题名和网传有一字之差,但能够对上年份和会议名称。但并未发现涉及任何与疫情预测相关的内容。","sourceUrl":"","score":191,"rumorType":0},{"id":14,"title":"戴多层口罩才能防住病毒?","mainSummary":"丁香医生团队辟谣:佩戴多层口罩可能造成呼吸不畅","summary":"","body":"不需要佩戴多层口罩。建议选择 N95/KN95 或普通外科口罩,并且一层就够,其他口罩防护效果不如这三种。佩戴多层口罩还可能造成呼吸不畅。\n","sourceUrl":"","score":190,"rumorType":0},{"id":178,"title":"确诊者家中的狗检测呈弱阳性?","mainSummary":"香港食卫局长陈肇始强调:虽查到弱阳性,但无证据显示狗会传播新冠病毒给人","summary":"","body":"据香港文汇网报道,香港渔护署发现一名新冠肺炎确诊患者饲养的宠物狗对病毒测试呈弱阳性反应。香港食物及卫生局长陈肇始 28 日强调,无证据显示狗会传播新冠病毒给人。她呼吁养宠物的市民注意个人卫生。\n\n陈肇始表示,这只狗会接受兽医的医学监察,并抽取样本化验,待测试结果呈阴性才会发还。","sourceUrl":"","score":190,"rumorType":1}]}catch(e){}</script><script id="getWikiList">try { window.getWikiList = {"pageNum":1,"pageSize":10,"total":7,"result":[{"id":1,"title":"什么是新型冠状病毒?","imgUrl":"","linkUrl":"https://ask.dxy.com/ama/index#/disease/24677/info/0","description":"此次流行的冠状病毒为一种新发现的冠状病毒,国际病毒分类委员会命名为 SARS-Cov-2。因为人群缺少对新型病毒株的免疫力,所以人群普遍易感。","sort":99},{"id":2,"title":"帮你排除感染症状","imgUrl":"https://img1.dxycdn.com/2020/0126/335/3393120598787233917-66.png","linkUrl":"https://ask.dxy.com/ama/index#/disease/24677/info/1","description":"出现什么症状可能感染了新型冠状病毒?如何在家做好自我隔离?","sort":50},{"id":3,"title":"冠状病毒怕什么?","imgUrl":"https://img1.dxycdn.com/2020/0126/584/3393140198870641951-73.jpg","linkUrl":"https://ask.dxy.com/ama/index#/disease/24677/info/2","description":"哪些人容易感染新型冠状病毒?什么人容易变成重症?新型冠状病毒致病性如何?","sort":45},{"id":7,"title":"做好预防免感染","imgUrl":"https://img1.dxycdn.com/2020/0127/314/3393234046053613437-73.jpg","linkUrl":"https://ask.dxy.com/ama/index#/disease/24677/info/6","description":"人口不密集的地方可以不带口罩吗?口罩能重复带吗?煮沸后能重复使用吗?","sort":40},{"id":6,"title":"日常防护要到位","imgUrl":"https://img1.dxycdn.com/2020/0126/366/3393121524352686218-66.png","linkUrl":"https://ask.dxy.com/ama/index#/disease/24677/info/5","description":"新型冠状病毒肺炎流行,外卖还能吃吗?买不到消毒液家里消毒用什么替代?","sort":35},{"id":4,"title":"如何确诊感染?","imgUrl":"https://img1.dxycdn.com/2020/0126/838/3393121081971054723-66.png","linkUrl":"https://ask.dxy.com/ama/index#/disease/24677/info/3","description":"新型冠状病毒肺炎患者有什么临床表现?医生如何识别并确诊新型冠状病毒肺炎?","sort":4},{"id":5,"title":"治疗方法早知道","imgUrl":"https://img1.dxycdn.com/2020/0126/500/3393121414831020167-66.png","linkUrl":"https://ask.dxy.com/ama/index#/disease/24677/info/4","description":"潜伏期能检查出来吗?感染了新型冠状病毒一定会得肺炎吗?新冠肺炎如何治疗?","sort":3}]}}catch(e){}</script><script id="getPV">try { window.getPV = 3884483212}catch(e){}</script><script id="getEntries">try { window.getEntries = [{"id":1,"configNo":1,"configName":"疫情报告","linkUrl":"https://mama.dxy.com/daily-previous-review?locationIds=999&from=todh5","imgUrl":"","picUrl":"https://img1.dxycdn.com/2020/0303/715/3399947621103181405-135.png","extLinkUrl":"https://mama.dxy.com/daily-previous-review?locationIds=999&from=todh5","subtitle":"每周五晚发布","clientUrl":""},{"id":2,"configNo":2,"configName":"健康好物 挑选指南","linkUrl":"https://mama.dxy.com/ncov-entry-200330","imgUrl":"","picUrl":"https://img1.dxycdn.com/2020/0221/550/3397927717311267685-135.png","extLinkUrl":"https://mama.dxy.com/ncov-entry-200330","subtitle":"防疫、失眠…你的困扰这里都有答案","clientUrl":""},{"id":3,"configNo":3,"configName":"母婴健康","linkUrl":"https://dxy.me/INZ2R3","imgUrl":"","picUrl":"https://img1.dxycdn.com/2020/0221/861/3397927687246311086-135.png","extLinkUrl":"https://dxy.me/cRDJfc","subtitle":"在家孕育指南","clientUrl":""},{"id":4,"configNo":4,"configName":"线上开药","linkUrl":"https://mp.weixin.qq.com/s/qHowks6IpzcCuyVfgAMaJA","imgUrl":"","picUrl":"https://img1.dxycdn.com/2020/0221/578/3397927811800552709-135.png","extLinkUrl":"https://mp.weixin.qq.com/s/qHowks6IpzcCuyVfgAMaJA","subtitle":"快递送货上门","clientUrl":""},{"id":5,"configNo":5,"configName":"在线问诊","linkUrl":"https://mp.weixin.qq.com/s/0PJGO35mUHnWDAIPIT6zmg","imgUrl":"","picUrl":"https://img1.dxycdn.com/2020/0221/518/3397927841865325260-135.png","extLinkUrl":"https://ask.dxy.com/view/i/volunteer_care/disease?dxa_adplatform=yqdtyzwbh5&svr_h5s=yqdtyzwbh5","subtitle":"三甲医生义诊","clientUrl":"https://ask.dxy.com/view/i/volunteer_care/disease?svr_h5s=yuanshengyz"}]}catch(e){}</script><script id="showPuppeteerUA">try { window.showPuppeteerUA = "DXYOPSTeam"}catch(e){}</script><script id="apierror">try { var prerender_error = "success" }catch(e){}</script>
将结果截取出来,转化jaon解析
public static void main(String[] args) throws IOException {
StringBuffer dstr = new StringBuffer("地区 现确诊 总确诊\\n");
JSONArray jsonObject = JSONArray.parseArray(Jsoup.connect("https://ncov.dxy.cn/ncovh5/view/pneumonia").get().getElementById("getAreaStat").toString().replaceAll("<.*?>", "").replaceAll("try . window.getAreaStat = ", "").replaceAll(".catch\\(e\\)\\{\\}", "").replaceAll("黑龙江省","黑龙").replaceAll("省","").replaceAll("市","").replaceAll("古自治区","").replaceAll("壮族自治区","").replaceAll("维吾尔自治区","").replaceAll("回族自治区","").replaceAll("自治区",""));
for(int i = 0 ; i < jsonObject.size() ; i++){
dstr.append(jsonObject.getJSONObject(i).getString("provinceName")+" - "+ jsonObject.getJSONObject(i).getString("currentConfirmedCount") + " " + jsonObject.getJSONObject(i).getString("confirmedCount")+"\\n");
}
System.out.println(dstr);
}
输出 结果
地区 现确诊 总确诊
台湾 - 58 440
香港 - 38 1050
吉林 - 25 134
上海 - 20 660
内蒙 - 18 209
北京 - 10 593
陕西 - 8 308
湖北 - 6 68134
山东 - 4 788
辽宁 - 3 149
广东 - 2 1589
黑龙 - 2 945
澳门 - 2 45
福建 - 1 356
河北 - 1 328
天津 - 1 191
河南 - 0 1276
浙江 - 0 1268
湖南 - 0 1019
安徽 - 0 991
江西 - 0 937
江苏 - 0 653
重庆 - 0 579
四川 - 0 561
广西 - 0 254
山西 - 0 198
云南 - 0 185
海南 - 0 168
贵州 - 0 147
甘肃 - 0 139
新疆 - 0 76
宁夏 - 0 75
青海 - 0 18
西藏 - 0 1
网友评论