【爬虫+可视化】Python爬取疫情数据,并做可视化展示_mport requests # 用于发送网页请求 import pandas as ___
import requests
import pandas as pd
from pyecharts.charts import Bar, Line
from pyecharts import options as opts
# 获取数据
def get_data(url):
response = requests.get(url)
return response.json()
# 解析数据
def parse_data(data):
records = data['records']
provinces = [record['provinceName'] for record in records]
confirmed_cases = [record['confirmedCount'] for record in records]
suspected_cases = [record['suspectedCount'] for record in records]
cured_cases = [record['curedCount'] for record in records]
dead_cases = [record['deadCount'] for record in records]
return provinces, confirmed_cases, suspected_cases, cured_cases, dead_cases
# 可视化数据
def visualize_data(provinces, confirmed_cases, suspected_cases, cured_cases, dead_cases):
# 确诊变异情况柱状图
bar = Bar()
bar.add_xaxis(provinces)
bar.add_yaxis("确诊", confirmed_cases)
bar.add_yaxis("疑似", suspected_cases)
bar.set_global_opts(title_opts=opts.TitleOpts(title="变异情况柱状图"))
bar.render("变异情况.html")
# 累计治愈和死亡情况折线图
line = Line()
line.add_xaxis(provinces)
line.add_yaxis("治愈", cured_cases, is_smooth=True)
line.add_yaxis("死亡", dead_cases, is_smooth=True)
line.set_global_opts(title_opts=opts.TitleOpts(title="治愈与死亡累计折线图"))
line.render("治愈与死亡.html")
# 主函数
def main():
url = "https://api.inews.qq.com/newsqa/v1/automation/modules/list?modules=FAutoCountry,WomWorld,AiCountry,WomAboard,CountryOther,OverseaFightForecast,WomAboardForecast,GlobalFight,ChinaFight,FightAroundWorld,FightCountry,FightProvince,FightType,MasksSupplies,FightForecast,FightTips,FightAroundWorldForecast,CountryOtherForecast&_=1615366747766"
data = get_data(url)
provinces, confirmed_cases, suspected_cases, cured_cases, dead_cases = parse_data(data)
visualize_data(provinces, confirmed_cases, suspected_cases, cured_cases, dead_cases)
if __name__ == "__main__":
main()
这段代码使用了pyecharts库来创建图表,并且使用requests库来发送HTTP请求从网络上获取数据。首先定义了一个获取数据的函数,然后解析数据,并定义了一个可视化数据的函数。最后,在主函数中调用这些函数来完成数据的爬取和可视化展示。
评论已关闭