Python爬虫:数学建模中获取全球COVID-19数据

好几天没有更新博客,参加了个疫情预测数学建模发现爬虫获取数据是真的很舒服不需要很多时间就可以得到你想要的数据,还可保存成你想要的格式,下边就记录一下。
在分享一个数据的网址自己认为是非常全也可以到处全球数据:Sources - Worldwide data on COVID-19

环境准备

  • 准备好Anaconda3和PyCharm3
  • 准备需要爬取的网址、反爬虫头部信息、国家信息、保存结果的excel文件路径
  • 导入爬虫需要的包
pip install openpyxl
pip install requests

爬虫代码

# 导入相关模块
import openpyxl
import requests
import time

# 记录爬虫开始的时间
start = time.time()
# 需要爬取的网址、反爬虫头部信息、国家信息、保存结果的excel文件路径
urlList = [
    # 美国疫情网址
    "https://api.inews.qq.com/newsqa/v1/automation/foreign/daily/list?country=%E7%BE%8E%E5%9B%BD&",
    # 意大利疫情网址
    "https://api.inews.qq.com/newsqa/v1/automation/foreign/daily/list?country=%E6%84%8F%E5%A4%A7%E5%88%A9&",
    # 法国疫情网址
    "https://api.inews.qq.com/newsqa/v1/automation/foreign/daily/list?country=%E6%B3%95%E5%9B%BD&",
    # 澳大利亚疫情网址
    "https://api.inews.qq.com/newsqa/v1/automation/foreign/daily/list?country=%E6%BE%B3%E5%A4%A7%E5%88%A9%E4%BA%9A&",
    # 韩国疫情网址
    "https://api.inews.qq.com/newsqa/v1/automation/foreign/daily/list?country=%E9%9F%A9%E5%9B%BD&",
    # 印度疫情网址
    "https://api.inews.qq.com/newsqa/v1/automation/foreign/daily/list?country=%E5%8D%B0%E5%BA%A6&"
]
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/83.0.4103.61 Safari/537.36 "
}
countryNameList = ["American",
                   "Italy",
                   "France",
                   "Australia",
                   "Korea",
                   "India"
                   ]


# 爬取数据并将数据进行保存
def dataSavedFunction(url):
    # 获取json文件并将文件转化为列表/字典格式
    response = requests.get(url, headers=headers)
    jsonResponse = response.json()
    # 遍历json处理后的数据并将相关数据添加到相应的空列表
    dataCollection = []
    for result in jsonResponse["data"]:
        dataCollection.append([
            result["date"],
            result["confirm"],
            result["dead"],
            result["heal"],
            result["confirm_add"]
        ])
    return dataCollection


# 将数据写入excel表格中
def dataToExcel():
   #try:
       # 读入一个空白excel文件
       wb = openpyxl.Workbook()
       for name in countryNameList:
           wb_sheet = wb.create_sheet(name)
           wb_sheet.append(["日期", "累积确诊", "累积死亡", "累计治愈", "现有新增确诊"])
           # 获取name的索引
           nameIndex = countryNameList.index(name)
           rows = dataSavedFunction(urlList[nameIndex])
           for j in rows:
               wb_sheet.append(j)
       # 保存相关文件内容
       wb.save("totalCrawlResult.xlsx")
       wb.close()
	#except PermissionError:
	   	#print("文件读写错误!该文件已经被打开,请关掉文件再试")

# 主函数调用
if __name__ == "__main__":
    dataToExcel()
    end = time.time()
    print("本次爬虫历时:", end - start, "秒")


执行程序3-4s就可以得到你需要的结果!
在这里插入图片描述

爬虫结果

在你的项目目录就可以看你需要的数据:
在这里插入图片描述
数据格式:在这里插入图片描述
冲冲冲!!!!!!!!!!!!!

  • 11
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值