全球疫情统计APP图表形式展示

全球疫情统计APP图表展示

将该任务分解成三部分来逐个实现:

①爬取全球疫情数据存储到云服务器的MySQL上

②在web项目里添加一个servlet,通过参数的传递得到对应的json数据

③设计AndroidAPP,通过时间和地名来访问服务器上的对应的servlet来获取json数据,然后将它与图表联系

 

第一步:由前面的web项目的积累,爬取全球的数据就很容易,利用Python爬虫爬取丁香医生上的数据,存储到服务器上的MySQL

全球疫情统计APP图表形式展示

全球疫情统计APP图表形式展示

from os import path import requests from bs4 import BeautifulSoup import json import pymysql import numpy as np import time url = 'https://ncov.dxy.cn/ncovh5/view/pneumonia?from=timeline&isappinstalled=0' #请求地址 headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'}#创建头部信息 response = requests.get(url,headers = headers) #发送网络请求 #print(response.content.decode('utf-8'))#以字节流形式打印网页源码 content = response.content.decode('utf-8') soup = BeautifulSoup(content, 'html.parser') listB = soup.find_all(name='script',attrs={"id":"getListByCountryTypeService2true"}) world_messages = str(listB)[95:-21] world_messages_json = json.loads(world_messages) # print(listB) # print(world_messages) worldList = [] for k in range(len(world_messages_json)): worldvalue = (world_messages_json[k].get('id'),time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), world_messages_json[k].get('continents'),world_messages_json[k].get('provinceName'), world_messages_json[k].get('cityName'),world_messages_json[k].get('currentConfirmedCount'), world_messages_json[k].get('suspectedCount'),world_messages_json[k].get('curedCount'),world_messages_json[k].get('deadCount'),world_messages_json[k].get('locationId'), world_messages_json[k].get('countryShortCode'),) worldList.append(worldvalue) db = pymysql.connect("139.129.221.11", "root", "fengge666", "yiqing", charset='utf8') cursor = db.cursor() #sql_clean_world = "TRUNCATE TABLE world_map" sql_world = "INSERT INTO world_map values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" worldTuple = tuple(worldList) # # try: # cursor.execute(sql_clean_world) # db.commit() # except: # print('执行失败,进入回调1') # db.rollback() try: cursor.executemany(sql_world,worldTuple) db.commit() except: print('执行失败,进入回调3') db.rollback() db.close()

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zydspg.html