Lesson 13

  • v1.0.0 2016.11 by David.Yi
  • v1.1 2020.5 edit by David Yi

本次内容要点

  • 访问网页
  • 调用接口
  • 思考一下

requests 包

requests 包是 python 目前最好用的网站内容访问包,设计上比较人性化,可以大大简化代码的复杂度。

网络访问、接口调用、网络检查、互联网爬虫等,都离不开 requests 包。

一般来说,python 自带的函数包还是做的非常不错的,但是也有例外,其中之一就是网络访问方面。与其说是 python 原生的网络访问函数包做的不够人性化,不如说是 requests 函数包做的太人性化了,设计的非常好,配合 python 自带的优雅属性,在 python 的迅速发展过程中起到了正向的作用。requests 之后,也有很多函数包打着人性化的旗子,甚至有一些滥用了。

使用 requests 包之前需要安装 requests。


In [1]:
# 获得一个网站的信息

import requests

r = requests.get('http://www.huifu.com')
print(r.content)


b'<!DOCTYPE html>\n<html lang="zh-CN">\n<head>\n    <meta charset="utf-8">\n    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">\n    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0" />\n    <meta name="format-detection" content="telphone=no, email=no"/>\n    <title>\xe6\xb1\x87\xe4\xbb\x98\xe5\xa4\xa9\xe4\xb8\x8b | \xe7\xa7\x91\xe6\x8a\x80\xe8\xae\xa9\xe6\x94\xaf\xe4\xbb\x98\xe6\x9b\xb4\xe6\x87\x82\xe4\xbd\xa0</title>\n    <link rel="shortcut icon" href="https://www.huifu.com/public/huifu-site/img/favicon.ico" />\n    <link href="https://www.huifu.com/public/huifu-site/css/style.css?ver=2020040302" rel="stylesheet"/>\n    <script src="https://www.huifu.com/public/huifu-site/js/jquery-3.3.1.min.js"></script>\n    <script src="https://www.huifu.com/public/huifu-site/js/main.js"></script>\n    <meta name="keywords" content="\xe6\xb1\x87\xe4\xbb\x98\xe5\xa4\xa9\xe4\xb8\x8b,\xe6\xb1\x87\xe4\xbb\x98\xe5\xa4\xa9\xe4\xb8\x8b\xe5\xae\x98\xe7\xbd\x91,\xe6\xb1\x87\xe4\xbb\x98" />\r\n<meta name="description" content="\xe6\xb1\x87\xe4\xbb\x98\xe5\xa4\xa9\xe4\xb8\x8b\xe6\x9c\x89\xe9\x99\x90\xe5\x85\xac\xe5\x8f\xb8\xef\xbc\x88\xe4\xbb\xa5\xe4\xb8\x8b\xe7\xae\x80\xe7\xa7\xb0\xe2\x80\x9c\xe6\xb1\x87\xe4\xbb\x98\xe5\xa4\xa9\xe4\xb8\x8b\xe2\x80\x9d\xef\xbc\x89\xe6\x98\xaf\xe4\xb8\x80\xe5\xae\xb6\xe4\xb8\xad\xe5\x9b\xbd\xe9\xa2\x86\xe5\x85\x88\xe7\x9a\x84\xe7\x8b\xac\xe7\xab\x8b\xe7\xac\xac\xe4\xb8\x89\xe6\x96\xb9\xe6\x94\xaf\xe4\xbb\x98\xe9\x9b\x86\xe5\x9b\xa2\xe5\x85\xac\xe5\x8f\xb8\xef\xbc\x8c\xe5\xa7\x8b\xe7\xbb\x88\xe4\xbb\xa5\xe5\x88\x9b\xe6\x96\xb0\xe7\x9a\x84\xe6\x94\xaf\xe4\xbb\x98\xe5\x92\x8c\xe9\x87\x91\xe8\x9e\x8d\xe7\xa7\x91\xe6\x8a\x80\xe8\xa7\xa3\xe5\x86\xb3\xe6\x96\xb9\xe6\xa1\x88\xef\xbc\x8c\xe4\xb8\xba\xe5\x95\x86\xe4\xb8\x9a\xe5\x92\x8c\xe6\xb6\x88\xe8\xb4\xb9\xe8\x80\x85\xe5\x88\x9b\xe9\x80\xa0\xe4\xbb\xb7\xe5\x80\xbc\xe3\x80\x82" /></head>\n<body>\n\n<nav>\n  <header>\n    <div class="header-menu">\n      <ul id="header-main-menu">\n        <li class="">\n          <a href="https://www.huifu.com/about-company/"><span>\xe5\x85\xb3\xe4\xba\x8e\xe6\xb1\x87\xe4\xbb\x98</span><i>+</i></a>\n          <ul>\n            <li class=""><a href="https://www.huifu.com/about-company/"><span>\xe5\x85\xac\xe5\x8f\xb8\xe7\xae\x80\xe4\xbb\x8b</span></a></li>\n            <li class=""><a href="https://www.huifu.com/about-history/"><span>\xe5\x8f\x91\xe5\xb1\x95\xe5\x8e\x86\xe7\xa8\x8b</span></a></li>\n            <li class=""><a href="https://www.huifu.com/about-leadership/"><span>\xe7\xae\xa1\xe7\x90\x86\xe5\x9b\xa2\xe9\x98\x9f</span></a></li>\n            <li class=""><a href="https://www.huifu.com/about-strengths/"><span>\xe6\xa0\xb8\xe5\xbf\x83\xe4\xbc\x98\xe5\x8a\xbf</span></a></li>\n          </ul>\n        </li>\n        <li class="">\n          <a href="https://www.huifu.com/business-payment/"><span>\xe5\x85\xac\xe5\x8f\xb8\xe4\xb8\x9a\xe5\x8a\xa1</span><i>+</i></a>\n          <ul>\n            <li class=""><a href="https://www.huifu.com/business-payment/"><span>\xe7\xbb\xbc\xe5\x90\x88\xe5\x95\x86\xe6\x88\xb7\xe6\x94\xb6\xe5\x8d\x95</span></a></li>\n            <li class=""><a href="https://www.huifu.com/business-solutions/"><span>\xe8\xa1\x8c\xe4\xb8\x9a\xe8\xa7\xa3\xe5\x86\xb3\xe6\x96\xb9\xe6\xa1\x88</span></a></li>\n            <li class=""><a href="https://www.huifu.com/business-saas/"><span>SaaS\xe6\x9c\x8d\xe5\x8a\xa1</span></a></li>\n            <li class=""><a href="https://www.huifu.com/business-international/"><span>\xe8\xb7\xa8\xe5\xa2\x83\xe5\x8f\x8a\xe5\x9b\xbd\xe9\x99\x85\xe4\xb8\x9a\xe5\x8a\xa1</span></a></li>\n          </ul>\n        </li>\n        <li class="">\n          <a href="https://www.huifu.com/ir-governance/"><span>\xe6\x8a\x95\xe8\xb5\x84\xe8\x80\x85\xe5\x85\xb3\xe7\xb3\xbb</span><i>+</i></a>\n          <ul>\n            <li class=""><a href="https://www.huifu.com/ir-governance/"><span>\xe5\x85\xac\xe5\x8f\xb8\xe7\xae\xa1\xe6\xb2\xbb</span></a></li>\n            <li class=""><a href="https://www.huifu.com/ir-announcements/"><span>\xe5\x85\xac\xe5\x91\x8a\xe5\x92\x8c\xe9\x80\x9a\xe5\x87\xbd</span></a></li>\n            <li class=""><a href="https://www.huifu.com/ir-reports/"><span>\xe5\xae\x9a\xe6\x9c\x9f\xe6\x8a\xa5\xe5\x91\x8a</span></a></li>\n            <li class=""><a href="https://www.huifu.com/ir-documents/"><span>\xe6\x8b\x9b\xe8\x82\xa1\xe6\x96\x87\xe4\xbb\xb6</span></a></li>\n            <li class=""><a href="https://www.huifu.com/ir-contact/"><span>\xe8\x81\x94\xe7\xb3\xbb\xe6\x96\xb9\xe5\xbc\x8f</span></a></li>\n          </ul>\n        </li>\n        <li class="index-logo-li">\n          <a href="https://www.huifu.com/cn/" class="index-logo">\n            <img src="/public/huifu-site/img/logo/white-cn.png" alt="" width="107" />\n            <img src="/public/huifu-site/img/logo/color-cn.png" alt="" width="107" />\n          </a>\n        </li>\n        <li class="">\n          <a href="https://www.huifu.com/news-company/"><span>\xe6\x96\xb0\xe9\x97\xbb\xe4\xb8\xad\xe5\xbf\x83</span><i>+</i></a>\n          <ul>\n            <li class=""><a href="https://www.huifu.com/news-company/"><span>\xe5\x85\xac\xe5\x8f\xb8\xe6\x96\xb0\xe9\x97\xbb</span></a></li>\n            <li class=""><a href="https://www.huifu.com/news-special-report/"><span>\xe4\xb8\x93\xe9\xa2\x98\xe6\x8a\xa5\xe9\x81\x93</span></a></li>\n            <li class=""><a href="https://www.huifu.com/news-compliance-area/"><span>\xe5\x90\x88\xe8\xa7\x84\xe4\xb8\x93\xe5\x8c\xba</span></a></li>\n          </ul>\n        </li>\n        <li class="">\n          <a href="https://www.huifu.com/contact-us/"><span>\xe8\x81\x94\xe7\xb3\xbb\xe6\x88\x91\xe4\xbb\xac</span></a>\n        </li>\n        <li class="index-menulast-li"><a href="javascript:;"><span>\xe6\x8a\x95\xe8\xb5\x84\xe8\x80\x85\xe5\x85\xb3\xe7\xb3\xbb</span></a></li>\n      </ul>\n      <ul class="menu-language">\n        <li class="on"><a href="/cn/"><span>\xe7\xae\x80</span></a></li>\n        <li class=""><a href="/hk/"><span>\xe7\xb9\x81</span></a></li>\n        <li class=""><a href="/en/" class="menu-language-en"><span>EN</span></a></li>\n      </ul>\n    </div>\n    <div class="header-bg"></div>\n    <div class="mode-desktop">\n      <div class="header-bg sub-menu"></div>\n    </div>\n  </header>\n  <div class="mobile-header mode-mobile">\n    <img src="/public/huifu-site/img/logo/white-cn.png" alt="\xe6\xb1\x87\xe4\xbb\x98\xe5\xa4\xa9\xe4\xb8\x8b Logo" width="107" />\n    <img src="/public/huifu-site/img/logo/color-cn.png" alt="\xe6\xb1\x87\xe4\xbb\x98\xe5\xa4\xa9\xe4\xb8\x8b Logo" width="107" />\n  </div>\n  <div class="mobile-header-mask"></div>\n  <button class="sidebar-toggle mode-mobile">\n    <span class="icon-bar"></span>\n    <span class="icon-bar"></span>\n    <span class="icon-bar"></span>\n  </button>\n</nav>\n\n<link href="/public/huifu-site/css/home.css" rel="stylesheet"/>\r\n<!--<a href="javascript:;" class="WAIC"><img src="/public/huifu-site/img/home-hgj.png" /></a>-->\r\n<section class="section-video">\r\n    <img class="primary-video mode-mobile" src="/public/huifu-site/img/home-750.jpg">\r\n    <video class="primary-video mode-desktop" autoplay="autoplay" loop="loop" muted="muted" preload="metadata" id="home-video"></video>\r\n  </section>\r\n  <div class="home-footer">\r\n    <p>\r\n      <a href="http://beian.miit.gov.cn" target="_blank">\xe6\xb2\xaaICP\xe5\xa4\x8706046402\xe5\x8f\xb7-28</a>\r\n      <!-- &nbsp; <a href="javascript:;">\xe6\xb2\xaa\xe5\x85\xac\xe7\xbd\x91\xe5\xae\x89\xe5\xa4\x87 31010102003432\xe5\x8f\xb7</a>-->\r\n    </p>\r\n    <p class="mode-desktop">\r\n      <span>\xc2\xa9 2006 - 2019 \xe6\xb1\x87\xe4\xbb\x98\xe5\xa4\xa9\xe4\xb8\x8b\xe6\x9c\x89\xe9\x99\x90\xe5\x85\xac\xe5\x8f\xb8 \xe7\x89\x88\xe6\x9d\x83\xe6\x89\x80\xe6\x9c\x89</span>\r\n    </p>\r\n    <p class="mode-mobile">\r\n      <span>\xc2\xa9 2019 \xe6\xb1\x87\xe4\xbb\x98\xe5\xa4\xa9\xe4\xb8\x8b\xe6\x9c\x89\xe9\x99\x90\xe5\x85\xac\xe5\x8f\xb8 \xe7\x89\x88\xe6\x9d\x83\xe6\x89\x80\xe6\x9c\x89</span>\r\n    </p>\r\n  </div>\r\n  <style>\r\n    html {overflow-y: hidden;}\r\n    .WAIC{\r\n      position:fixed;\r\n      top:72px;\r\n      left:167px;\r\n      z-index:90\r\n    }\r\n    .WAIC img{\r\n      width:150px;height:150px;\r\n    }\r\n    @media (max-width: 767px){\r\n      .WAIC{\r\n        margin:auto;\r\n        left:0;\r\n        right:0;\r\n        text-align:center;\r\n      }\r\n    }\r\n.video-mask{\r\n      position: absolute;\r\n      top: 0px;\r\n      width: 100%;\r\n      height: 100%;\r\n      background:#000;\r\n      opacity: 0.8;\r\n      z-index: 200;\r\n      display: none;\r\n    }\r\n    .home-popup-video{\r\n      position: absolute;\r\n      margin:auto;\r\n      left:0;\r\n      right:0;\r\n      top: 50%;\r\n      margin-top: -240px;\r\n      z-index: 500;\r\n      width: 854px;\r\n      display: none;\r\n    }\r\n    #popup-video{\r\n      width: 854px;\r\n    }\r\n    .close{\r\n      display: block;\r\n      position: absolute;\r\n      top: -15px;\r\n      right: -15px;\r\n      width: 30px;\r\n      height: 30px;\r\n      z-index: 800;\r\n      cursor:pointer;\r\n    }\r\n  </style>\r\n<script src="/public/huifu-site/js/home.js"></script>\r\n<!-- \xe6\x96\xb0\xe5\xa2\x9e\xe8\xa7\x86\xe9\xa2\x91 -->\r\n  <div class="video-mask"></div>\r\n  <section class="home-popup-video clearfloat">\r\n    <img src="/public/huifu-site/img/special/AI/close.png" class="close" alt="">\r\n  </section>\r\n  <script>\r\n    $(function () {\r\n      // \xe7\x82\xb9\xe5\x87\xbb\xe5\x9b\xbe\xe7\x89\x87\xef\xbc\x8c\xe5\x8a\xa0\xe8\xbd\xbd\xe8\xa7\x86\xe9\xa2\x91\r\n      $(document).on("click",".WAIC",function () {\r\n        $(".video-mask,.home-popup-video").fadeIn();\r\n        // \xe6\x9a\x82\xe5\x81\x9c\xe9\xa6\x96\xe9\xa1\xb5\xe5\xa4\xa7\xe8\xa7\x86\xe9\xa2\x91\r\n        var homeVideo = document.getElementById("home-video");\r\n        homeVideo.pause();\r\n        $(".home-popup-video").append(`<video src="https://jarvisfile-publiccdn.oss-cn-shanghai.aliyuncs.com/app-0ed0d2e7-26d3-4b25-915c-85d12d0c8236/huifu/site/home-video-1-1.mp4" controls="controls" autoplay="autoplay" \r\n         loop="loop" preload="metadata" id="popup-video"></video>`)\r\n      })\r\n      // \xe7\x82\xb9\xe5\x87\xbb\xe5\x85\xb3\xe9\x97\xad\xef\xbc\x8c\xe5\x8e\xbb\xe6\x8e\x89\xe8\xa7\x86\xe9\xa2\x91\r\n      $(document).on("click",".home-popup-video .close",function () {\r\n        // \xe6\x92\xad\xe6\x94\xbe\xe9\xa6\x96\xe9\xa1\xb5\xe5\xa4\xa7\xe8\xa7\x86\xe9\xa2\x91\r\n        var homeVideo = document.getElementById("home-video");\r\n        homeVideo.play();\r\n        $(".video-mask,.home-popup-video").fadeOut();\r\n        $("#popup-video").remove();\r\n      })\r\n    })\r\n  </script>\n<div class="seciton-news">\n    <div class="seciton-news-bg">\n        <div class="section-news-inner">\n            <p class="section-news-tag">\xe6\x96\xb0\xe9\x97\xbb<span>|</span></p>\n            <div class="section-news-sildeBox">\n                <div class="section-news-moveBox" style="left: -1649px;">\n                                        <p class="section-news-list">\n                        <a href="https://jfile.cloudpnr.com/app-0ed0d2e7-26d3-4b25-915c-85d12d0c8236/huifu/site/2020032501417_c.pdf" target="_blank">\n                            <span class="section-news-date">2020-03-25</span>\n                            <span class="section-news-title">\xe6\x88\xaa\xe8\x87\xb32019 \xe5\xb9\xb412 \xe6\x9c\x8831 \xe6\x97\xa5\xe6\xad\xa2\xe5\xb9\xb4\xe5\xba\xa6\xe5\xb9\xb4\xe5\xba\xa6\xe4\xb8\x9a\xe7\xbb\xa9\xe5\x85\xac\xe5\x91\x8a</span>\n                        </a>\n                    </p>\n                                        <p class="section-news-list">\n                        <a href="https://jfile.cloudpnr.com/app-0ed0d2e7-26d3-4b25-915c-85d12d0c8236/huifu/site/C20200228-huifu_Announcement-_Board-Meeting_cn.pdf" target="_blank">\n                            <span class="section-news-date">2020-03-13</span>\n                            <span class="section-news-title">\xe8\x91\xa3\xe4\xba\x8b\xe4\xbc\x9a\xe4\xbc\x9a\xe8\xae\xae\xe9\x80\x9a\xe7\x9f\xa5</span>\n                        </a>\n                    </p>\n                                        <p class="section-news-list">\n                        <a href="https://jfile.cloudpnr.com/app-0ed0d2e7-26d3-4b25-915c-85d12d0c8236/huifu/site/c_huifu_monthly-return_202002.pdf" target="_blank">\n                            <span class="section-news-date">2020-03-03</span>\n                            <span class="section-news-title">\xe6\x88\xaa\xe8\x87\xb32020\xe5\xb9\xb42\xe6\x9c\x8829\xe6\x97\xa5\xe6\xad\xa2\xe6\x9c\x88\xe4\xbb\xbd\xe4\xb9\x8b\xe8\x82\xa1\xe4\xbb\xbd\xe5\x8f\x91\xe8\xa1\x8c\xe4\xba\xba\xe7\x9a\x84\xe8\xaf\x81\xe5\x88\xb8\xe5\x8f\x98\xe5\x8a\xa8\xe6\x9c\x88\xe6\x8a\xa5\xe8\xa1\xa8</span>\n                        </a>\n                    </p>\n                                        <p class="section-news-list">\n                        <a href="https://jfile.cloudpnr.com/app-0ed0d2e7-26d3-4b25-915c-85d12d0c8236/huifu/site/2020021000607_c.pdf" target="_blank">\n                            <span class="section-news-date">2020-02-10</span>\n                            <span class="section-news-title">\xe8\x87\xaa\xe6\x84\xbf\xe6\x80\xa7\xe5\x85\xac\xe5\x91\x8a-\xe6\x8d\x90\xe6\xac\xbe\xe7\x94\xa8\xe4\xba\x8e\xe6\x8a\x97\xe5\x87\xbb\xe6\x96\xb0\xe5\x9e\x8b\xe5\x86\xa0\xe7\x8a\xb6\xe7\x97\x85\xe6\xaf\x92\xe7\x96\xab\xe6\x83\x85</span>\n                        </a>\n                    </p>\n                                        <p class="section-news-list">\n                        <a href="https://www.huifu.com/news-company/2389.html" target="_blank">\n                            <span class="section-news-date">2020-02-10</span>\n                            <span class="section-news-title">\xe4\xbc\x97\xe5\xbf\x97\xe6\x88\x90\xe5\x9f\x8e \xe6\xb1\x87\xe4\xbb\x98\xe5\xa4\xa9\xe4\xb8\x8b\xe5\xae\xa3\xe5\xb8\x83\xe6\x8d\x90\xe8\xb5\xa0520\xe4\xb8\x87\xe5\x85\x83\xe9\xa9\xb0\xe6\x8f\xb4\xe7\x96\xab\xe6\x83\x85\xe9\x98\xb2\xe6\x8e\xa7</span>\n                        </a>\n                    </p>\n                                    </div>\n            </div>\n        </div>\n    </div>\n\n</div>\n\n\n\n</body>\n</html>\n\n\n<!-- Dynamic page generated in 0.158 seconds. -->\n<!-- Cached page generated by WP-Super-Cache on 2020-04-03 10:24:01 -->\n\n<!-- Compression = gzip -->'

In [2]:
print(r.headers)


{'Server': 'Tengine', 'Content-Type': 'text/html; charset=UTF-8', 'Content-Length': '10979', 'Connection': 'keep-alive', 'Date': 'Tue, 05 May 2020 07:11:03 GMT', 'Set-Cookie': 'acw_tc=65e321a715886626634018176ee2d179c2065e236f01385331cd67a3b7;path=/;HttpOnly;Max-Age=2678401', 'ALIWAF-CACHE': 'HIT', 'Ali-Swift-Global-Savetime': '1588662663', 'Via': 'cache14.l2cn1809[65,200-0,M], cache4.l2cn1809[66,0], vcache2.cn2539[74,200-0,M], vcache19.cn2539[78,0]', 'X-Cache': 'MISS TCP_MISS dirn:-2:-2', 'X-Swift-SaveTime': 'Tue, 05 May 2020 07:11:03 GMT', 'X-Swift-CacheTime': '0', 'Timing-Allow-Origin': '*', 'EagleId': '65e321a715886626634018176e'}

下载文件

使用requets 可以很方便的获得网站中的图片、文件等。下面只是简单的举例,下载 baidu 的 logo 文件。


In [3]:
# 下载文件 使用 requests
# baidu 的 logo 文件: http://home.baidu.com/resource/r/home/img/logo-yy.gif  

import requests

url = 'http://home.baidu.com/resource/r/home/img/logo-yy.gif'

r = requests.get(url)
with open("files/baidu_logo.gif", "wb") as code:
    code.write(r.content)
    print('download ok')


download ok

读取接口

我们来做一个读取新冠疫情数据的demo。

平时我们说的接口,可以简单的理解为满足一定的认证方式后,通过输入参数的值,获得需要的内容。认证方式、入参、出参这些都是事先约定的。包括接口文档、参数列表、自动联调等这些,在目前 python 的接口开发中都是可以自动生成的。python 本身开发接口非常容易,我们另外专门讲述。相对来说,读取调用接口更为常见。


In [4]:
# demo for infection/region
# input region, start_date, then get data
# 接口:感染/国家地区

import requests

# API url
url = 'https://covid-19.adapay.tech/api/v1/'
# token, can call register function get the API token
token = '497115d0c2ff9586bf0fe03088cfdbe2'

# region or country
region='US'

# headers, need the API token
headers = {
    'token': token
}

# the params
payload = {
    'region': region,
    'start_date':'2020-04-24'
}

# call requets to load 
r = requests.get(url+'infection/region', params=payload, headers=headers)

data = r.json()

print(data)

print(type(data))


{'code': '90000', 'message': 'success', 'data': {'region': {'US': {'2020-04-24': {'confirmed_add': 36163, 'deaths_add': 1995, 'recovered_add': 18876, 'confirmed': 905284, 'deaths': 51949, 'recovered': 99079}}}}}
<class 'dict'>

In [5]:
# 获得指定key 的内容,实际上是字典,因此可以一层层嵌套访问

print(data['data']['region']['US']['2020-04-24']['confirmed'])


905284

In [6]:
# 我们模拟一个实际的使用方式,获得10天的数据

# demo for infection/region
# input region, start_date, end_date, then get data
# 接口:感染/国家地区

import requests

# API url
url = 'https://covid-19.adapay.tech/api/v1/'
# token, can call register function get the API token
token = '497115d0c2ff9586bf0fe03088cfdbe2'

# region or country
region='US'

# headers, need the API token
headers = {
    'token': token
}

# the params
payload = {
    'region': region,
    'start_date':'2020-04-24',
    'end_date':'2020-05-03'
}

# call requets to load 
r = requests.get(url+'infection/region', params=payload, headers=headers)

data = r.json()

print(data)


{'code': '90000', 'message': 'success', 'data': {'region': {'US': {'2020-04-24': {'confirmed_add': 36163, 'deaths_add': 1995, 'recovered_add': 18876, 'confirmed': 905284, 'deaths': 51949, 'recovered': 99079}, '2020-04-25': {'confirmed_add': 32821, 'deaths_add': 1806, 'recovered_add': 1293, 'confirmed': 938105, 'deaths': 53755, 'recovered': 100372}, '2020-04-26': {'confirmed_add': 27629, 'deaths_add': 1126, 'recovered_add': 6616, 'confirmed': 965734, 'deaths': 54881, 'recovered': 106988}, '2020-04-27': {'confirmed_add': 22414, 'deaths_add': 1378, 'recovered_add': 4436, 'confirmed': 988148, 'deaths': 56259, 'recovered': 111424}, '2020-04-28': {'confirmed_add': 24385, 'deaths_add': 2096, 'recovered_add': 4512, 'confirmed': 1012533, 'deaths': 58355, 'recovered': 115936}, '2020-04-29': {'confirmed_add': 27327, 'deaths_add': 2612, 'recovered_add': 4784, 'confirmed': 1039860, 'deaths': 60967, 'recovered': 120720}, '2020-04-30': {'confirmed_add': 29515, 'deaths_add': 2029, 'recovered_add': 33227, 'confirmed': 1069375, 'deaths': 62996, 'recovered': 153947}, '2020-05-01': {'confirmed_add': 34037, 'deaths_add': 1947, 'recovered_add': 10068, 'confirmed': 1103412, 'deaths': 64943, 'recovered': 164015}, '2020-05-02': {'confirmed_add': 29078, 'deaths_add': 1426, 'recovered_add': 11367, 'confirmed': 1132490, 'deaths': 66369, 'recovered': 175382}, '2020-05-03': {'confirmed_add': 25501, 'deaths_add': 1313, 'recovered_add': 4770, 'confirmed': 1157991, 'deaths': 67682, 'recovered': 180152}}}}}

In [7]:
# 截取需要的字典内容
dict1 = data['data']['region']['US']
print(dict1)

print('---')

# 根据字典进行遍历
list1 = []
list2 = []
for key, value in dict1.items():
    print(key,value)
    list1.append(value['confirmed'])
    list2.append(key[5:10])
    
print('---')
print(list1)

print('---')
print(list2)


{'2020-04-24': {'confirmed_add': 36163, 'deaths_add': 1995, 'recovered_add': 18876, 'confirmed': 905284, 'deaths': 51949, 'recovered': 99079}, '2020-04-25': {'confirmed_add': 32821, 'deaths_add': 1806, 'recovered_add': 1293, 'confirmed': 938105, 'deaths': 53755, 'recovered': 100372}, '2020-04-26': {'confirmed_add': 27629, 'deaths_add': 1126, 'recovered_add': 6616, 'confirmed': 965734, 'deaths': 54881, 'recovered': 106988}, '2020-04-27': {'confirmed_add': 22414, 'deaths_add': 1378, 'recovered_add': 4436, 'confirmed': 988148, 'deaths': 56259, 'recovered': 111424}, '2020-04-28': {'confirmed_add': 24385, 'deaths_add': 2096, 'recovered_add': 4512, 'confirmed': 1012533, 'deaths': 58355, 'recovered': 115936}, '2020-04-29': {'confirmed_add': 27327, 'deaths_add': 2612, 'recovered_add': 4784, 'confirmed': 1039860, 'deaths': 60967, 'recovered': 120720}, '2020-04-30': {'confirmed_add': 29515, 'deaths_add': 2029, 'recovered_add': 33227, 'confirmed': 1069375, 'deaths': 62996, 'recovered': 153947}, '2020-05-01': {'confirmed_add': 34037, 'deaths_add': 1947, 'recovered_add': 10068, 'confirmed': 1103412, 'deaths': 64943, 'recovered': 164015}, '2020-05-02': {'confirmed_add': 29078, 'deaths_add': 1426, 'recovered_add': 11367, 'confirmed': 1132490, 'deaths': 66369, 'recovered': 175382}, '2020-05-03': {'confirmed_add': 25501, 'deaths_add': 1313, 'recovered_add': 4770, 'confirmed': 1157991, 'deaths': 67682, 'recovered': 180152}}
---
2020-04-24 {'confirmed_add': 36163, 'deaths_add': 1995, 'recovered_add': 18876, 'confirmed': 905284, 'deaths': 51949, 'recovered': 99079}
2020-04-25 {'confirmed_add': 32821, 'deaths_add': 1806, 'recovered_add': 1293, 'confirmed': 938105, 'deaths': 53755, 'recovered': 100372}
2020-04-26 {'confirmed_add': 27629, 'deaths_add': 1126, 'recovered_add': 6616, 'confirmed': 965734, 'deaths': 54881, 'recovered': 106988}
2020-04-27 {'confirmed_add': 22414, 'deaths_add': 1378, 'recovered_add': 4436, 'confirmed': 988148, 'deaths': 56259, 'recovered': 111424}
2020-04-28 {'confirmed_add': 24385, 'deaths_add': 2096, 'recovered_add': 4512, 'confirmed': 1012533, 'deaths': 58355, 'recovered': 115936}
2020-04-29 {'confirmed_add': 27327, 'deaths_add': 2612, 'recovered_add': 4784, 'confirmed': 1039860, 'deaths': 60967, 'recovered': 120720}
2020-04-30 {'confirmed_add': 29515, 'deaths_add': 2029, 'recovered_add': 33227, 'confirmed': 1069375, 'deaths': 62996, 'recovered': 153947}
2020-05-01 {'confirmed_add': 34037, 'deaths_add': 1947, 'recovered_add': 10068, 'confirmed': 1103412, 'deaths': 64943, 'recovered': 164015}
2020-05-02 {'confirmed_add': 29078, 'deaths_add': 1426, 'recovered_add': 11367, 'confirmed': 1132490, 'deaths': 66369, 'recovered': 175382}
2020-05-03 {'confirmed_add': 25501, 'deaths_add': 1313, 'recovered_add': 4770, 'confirmed': 1157991, 'deaths': 67682, 'recovered': 180152}
---
[905284, 938105, 965734, 988148, 1012533, 1039860, 1069375, 1103412, 1132490, 1157991]
---
['04-24', '04-25', '04-26', '04-27', '04-28', '04-29', '04-30', '05-01', '05-02', '05-03']

In [8]:
# 绘制一个折线图

import matplotlib.pyplot as plt
 
plt.plot(list2,list1)
plt.show()



In [ ]: