Python作业8-网络爬虫

1.编程实现:利用requests爬虫库和json解析库实现疫情数据爬取。

已知:

(1)爬取网址:

url='https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list?modules=statisGradeCityDetail,diseaseh5Shelf'

(2)爬取授权:

headers={

    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36'

}

(3)爬取解析:

json_data = response.json()['data']['diseaseh5Shelf']['areaTree'][0]['children']

2. 编程实现:利用xlrd库将以上爬取的数据('地区'、'新增确诊'、'治愈人数'、'死亡人数'、'现存人数'、'累计确诊')存入excel文件中。

3. 编程实现:利用xlwt库读取上述excel文件中的'地区'和'新增确诊'两列数据,利用matplotlib库折线图可视化这两种数据。

import requests
import json
import pandas as pd
import matplotlib.pyplot as plt
import xlrd
import xlwt
# 创建一个Excel
book = xlwt.Workbook(encoding='utf-8')
sheet = book.add_sheet('疫情显示')
# 进行爬虫操作
url = 'https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list?modules=statisGradeCityDetail,diseaseh5Shelf'
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36'
}
response = requests.get(url=url, headers=headers)
json_data = response.json()['data']['diseaseh5Shelf']['areaTree'][0]['children']
col = ('地区', '新增确诊', '治愈人数', '死亡人数', '现存人数', '累计确诊')
# 将爬取的数据放入Excel中
for i in range(6):
    sheet.write(0, i, col[i])
for i in range(len(json_data)):
    sheet.write(i+1, 0, json_data[i]['name'])
    sheet.write(i+1, 1, json_data[i]['today']['confirm'])
    sheet.write(i+1, 2, json_data[i]['total']['heal'])
    sheet.write(i+1, 3, json_data[i]['total']['dead'])
    sheet.write(i+1, 4, json_data[i]['total']['nowConfirm'])
    sheet.write(i+1, 5, json_data[i]['total']['confirm'])
book.save('疫情.xlsx')

# 读取Excel中的值
du = xlrd.open_workbook('疫情.xlsx')
sheet1 = du.sheets()[0]
list1 = sheet1.col_values(1)[1:]
list2 = sheet1.col_values(0)[1:]
len1 = len(list1)


# 作图
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.figure(figsize=(20, 5))
plt.plot(range(1, len1+1), sheet1.col_values(1)[1:])
plt.xticks(range(1, len1+1), sheet1.col_values(0)[1:])
plt.xlabel("地区")
plt.ylabel("新增确诊")
plt.title("疫情显示")
plt.savefig("疫情.png")
plt.show()

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值