python爬取全球疫情数据

最近参加了江苏省的数模省赛,做了一个题目,是关于疫情的,我们选择腾讯的疫情数据作为爬取对象,相关的代码如下。为了测试爬虫历时,我还记录了打印操作。

有小伙伴评论说except调试问题,关于这个问题,当初这个调试只是防止excel文件没关闭就进行写入数据的异常,其实用处不大,可以注释掉我的try except部分。整个程序本身是完整的,这个小问题可以忽略。

# 导入相关模块
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, "秒")

  • 39
    点赞
  • 124
    收藏
    觉得还不错? 一键收藏
  • 23
    评论
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值