前言
整个世界正被大流行困扰着,不同国家拿出了不同的应对策略,也取得了不同效果。这也是本文的脑洞来源,打算研究一下各国在医疗基础设置上的开支,对几个国家的医疗费用进行数据可视化。
由于没有找到最近一年的可靠数据来源,所以这里使用的是2016年的数据。数据清楚哪个国家花得最多、哪个国家花得最少。我一直想试试在Python中网络抓取和数据可视化,这算是个不错的项目。虽然手动将数据输入Excel肯定快得多,但是这样就不会有宝贵的机会来练习一些技能了。
数据科学就是利用各种工具包来解决问题,网络抓取和正则表达式是我需要研究的两个领域。结果简短但复杂,这一项目展示了如何将三种技术结合起来解决数据科学问题。
要求
网络抓取主要分为两部分:
通过发出HTTP请求来获取数据
通过解析HTMLDOM来提取重要数据
库和工具
Requests能够非常简单地发送HTTP请求。
Pandas是一个Python包,提供快速、灵活和有表现力的数据结构。
Web Scraper可以帮助在不设置任何自动化浏览器的情况下抓取动态网站。
Beautiful Soup是一个Python库,用于从HTML和XML文件中提取数据。
matplotlib是一个综合的库,用于在Python中创建静态、动画和交互式可视化效果。
设置
设置非常简单,只需创建一个文件夹,并安装BeautifulSoup和Requests。此处假设已经安装了Python3.x,再根据指令来创建文件夹并安装库。
mkdir scraper pip install beautifulsoup4 pip install requests pip install matplotlib pip install pandas
现在,在该文件夹中创建一个任意名称的文件。这里用的是scraping.py.,然后在文件中导入Beautiful Soup和 requests,如下所示:
import pandas as pd from bs4 import BeautifulSoup import matplotlib.pyplot as plt import requests
抓取的内容:国家名;人均开销。
网络抓取
现在,所有scraper设置都已准备好,应向target URL发出GET请求以获得原始HTML数据。
r =requests.get( https://api.scrapingdog.com/scrape?api_key=<YOUR_API_KEY>&url=https://data.worldbank.org/indicator/SH.XPD.CHEX.PC.CD?most_recent_value_desc=false&dynamic=true ).text
这将得出target URL的HTML代码,我们必须使用Beautiful Soup来解析HTML。
soup = BeautifulSoup(r,’html.parser’) country=list() expense=list()
笔者用两张空表来存储国家名和每个国家24小时内的开支。可以看到,每个国家都存储在一个“项目”标签中,把所有的项目标签都存储在一张列表中。
try: Countries=soup.find_all(“div”,{“class”:”item”}) except: Countries=None
世界上有190个国家,为每个国家的医疗开支运行一个for循环:
for i in range(0,190): country.append(Countries[i+1].find_all(“div”,{“class”:None})[0].text.replace(“”,””)) expense.append(round(float(Countries[i+1].find_all(“div”,{“class”:None})[2].text.replace(“”,””).replace(‘,’,’’)))/365) Data = {‘country’:country,’expense’: expense}
因为我想看看这些国家每天是如何花钱的,所以把这笔费用除以365。如果把给定的数据直接除以365,这可能会更容易些,但这样就没有学习的意义了。现在的“数据”看起来是这样的:
{ country : [ Central AfricanRepublic , Burundi , Mozambique , Congo, Dem. Rep. , Gambia, The , Niger , Madagascar , Ethiopia , Malawi , Mali , Eritrea , Benin , Chad , Bangladesh , Tanzania , Guinea , Uganda , Haiti , Togo , Guinea-Bissau , Pakistan , Burkina Faso , Nepal , Mauritania , Rwanda , Senegal , PapuaNew Guinea , Lao PDR , Tajikistan , Zambia , Afghanistan , Comoros , Myanmar , India , Cameroon , Syrian Arab Republic , Kenya , Ghana ,"Cote d Ivoire", Liberia , Djibouti , Congo, Rep. , Yemen, Rep. , Kyrgyz Republic , Cambodia , Nigeria , Timor-Leste , Lesotho , SierraLeone , Bhutan , Zimbabwe , Angola , Sao Tome and Principe , SolomonIslands , Vanuatu , Indonesia , Vietnam , Philippines , Egypt, Arab Rep. , Uzbekistan , Mongolia , Ukraine , Sudan , Iraq , Sri Lanka , CaboVerde , Moldova , Morocco , Fiji , Kiribati , Nicaragua , Guyana , Honduras , Tonga , Bolivia , Gabon , Eswatini , Thailand , Jordan , Samoa , Guatemala , St. Vincent and the Grenadines , Tunisia , Algeria , Kazakhstan , Azerbaijan , Albania , Equatorial Guinea , El Salvador , Jamaica , Belize , Georgia , Libya , Peru , Belarus , Paraguay , NorthMacedonia , Colombia , Suriname , Armenia , Malaysia , Botswana , Micronesia, Fed. Sts. , China , Namibia , Dominican Republic , Iran,Islamic Rep. , Dominica , Turkmenistan , South Africa , Bosnia andHerzegovina , Mexico , Turkey , Russian Federation , Romania , St. Lucia , Serbia , Ecuador , Tuvalu , Grenada , Montenegro , Mauritius , Seychelles , Bulgaria , Antigua and Barbuda , Brunei Darussalam , Oman , Lebanon , Poland , Marshall Islands , Latvia , Croatia , Costa Rica , St. Kitts and Nevis , Hungary , Argentina , Cuba , Lithuania , Nauru , Brazil , Panama , Maldives , Trinidad and Tobago , Kuwait , Bahrain , Saudi Arabia , Barbados , Slovak Republic , Estonia , Chile , CzechRepublic , United Arab Emirates , Uruguay , Greece , Venezuela, RB , Cyprus , Palau , Portugal , Qatar , Slovenia , Bahamas, The , Korea,Rep. , Malta , Spain , Singapore , Italy , Israel , Monaco , SanMarino , New Zealand , Andorra , United Kingdom , Finland , Belgium , Japan , France , Canada , Austria , Germany , Netherlands , Ireland , Australia , Iceland , Denmark , Sweden , Luxembourg , Norway , Switzerland , United States , World ], expense : [0.043835616438356165,0.049315068493150684, 0.052054794520547946, 0.057534246575342465,0.057534246575342465, 0.06301369863013699, 0.06575342465753424,0.07671232876712329, 0.0821917808219178, 0.0821917808219178,0.0821917808219178, 0.0821917808219178, 0.08767123287671233,0.09315068493150686, 0.09863013698630137, 0.10136986301369863,0.10410958904109589, 0.10410958904109589, 0.10684931506849316,0.10684931506849316, 0.1095890410958904, 0.11232876712328767,0.1232876712328767, 0.12876712328767123, 0.13150684931506848,0.14520547945205478, 0.1506849315068493, 0.1506849315068493, 0.15342465753424658,0.15616438356164383, 0.15616438356164383, 0.16164383561643836,0.16986301369863013, 0.1726027397260274, 0.17534246575342466,0.18082191780821918, 0.18082191780821918, 0.1863013698630137,0.1863013698630137, 0.1863013698630137, 0.1917808219178082, 0.1917808219178082,0.19726027397260273, 0.2, 0.2136986301369863, 0.21643835616438356,0.2191780821917808, 0.2356164383561644, 0.2356164383561644, 0.2493150684931507,0.25753424657534246, 0.2602739726027397, 0.2876712328767123, 0.29041095890410956,0.3013698630136986, 0.30684931506849317, 0.336986301369863,0.35342465753424657, 0.3589041095890411, 0.3698630136986301,0.3863013698630137, 0.3863013698630137, 0.41643835616438357,0.4191780821917808, 0.4191780821917808, 0.43561643835616437, 0.4684931506849315,0.4684931506849315, 0.4931506849315068, 0.5150684931506849, 0.5150684931506849,0.5260273972602739, 0.547945205479452, 0.5561643835616439, 0.5835616438356165,0.6027397260273972, 0.6054794520547945, 0.6082191780821918, 0.6136986301369863,0.6219178082191781, 0.6602739726027397, 0.684931506849315, 0.7013698630136986,0.7123287671232876, 0.7178082191780822, 0.7342465753424657, 0.7452054794520548,0.7698630136986301, 0.8054794520547945, 0.810958904109589, 0.8328767123287671,0.8438356164383561, 0.8575342465753425, 0.8657534246575342, 0.8712328767123287,0.8958904109589041, 0.8986301369863013, 0.9315068493150684, 0.9753424657534246,0.9835616438356164, 0.9917808219178083, 1.0410958904109588, 1.0602739726027397,1.0904109589041096, 1.104109589041096, 1.1342465753424658, 1.1369863013698631,1.1479452054794521, 1.158904109589041, 1.1726027397260275, 1.2164383561643837,1.2657534246575342, 1.284931506849315, 1.284931506849315, 1.3041095890410959,1.3424657534246576, 1.3534246575342466, 1.3835616438356164, 1.389041095890411,1.4136986301369863, 1.4575342465753425, 1.515068493150685, 1.6356164383561644,1.6767123287671233, 1.7068493150684931, 1.7287671232876711, 1.7753424657534247,1.8136986301369864, 2.2164383561643834, 2.3315068493150686, 2.3945205479452056,2.421917808219178, 2.4356164383561643, 2.5506849315068494, 2.5835616438356164,2.6164383561643834, 2.66027397260274, 2.706849315068493, 2.7726027397260276,2.7835616438356166, 2.852054794520548, 2.871232876712329, 2.915068493150685,2.926027397260274, 3.010958904109589, 3.1424657534246574, 3.1890410958904107,3.23013698630137, 3.2465753424657535, 3.263013698630137, 3.621917808219178,3.6246575342465754, 3.778082191780822, 4.13972602739726, 4.323287671232877,4.476712328767123, 4.586301369863014, 4.934246575342466, 5.005479452054795,5.024657534246575, 5.027397260273973, 5.6, 6.3780821917808215,6.5479452054794525, 6.745205479452054, 7.504109589041096, 7.772602739726027,8.054794520547945, 8.254794520547945, 10.26027397260274, 10.506849315068493,10.843835616438357, 11.27945205479452, 11.367123287671232, 11.597260273972603,11.67945205479452, 12.213698630136987, 12.843835616438357, 12.915068493150685,12.991780821917809, 13.038356164383561, 13.704109589041096, 13.873972602739727,15.24931506849315, 15.646575342465754, 17.18082191780822, 20.487671232876714,26.947945205479453, 27.041095890410958, 2.8109589041095893]}
数据帧
绘制图表之前,必须使用Pandas准备一个数据帧。首先我们得明确DataFrame是什么:DataFrame是一个二维大小可变的、潜在的异构表格式数据结构,带有标记的轴(行和列)。创造一个数据帧非常简单直接:
df = pd.DataFrame(Data,columns=[‘country’, ‘expense’])
可视化
我们大部分时间都花在收集和格式化数据上,现在到了做图的时候啦,可以使用matplotlib和seaborn 来可视化数据。如果不太在意美观,可以使用内置的数据帧绘图方法快速显示结果:
df.plot(kind = ‘bar’, x=’country’, y=’expense’) plt.show()
现在,结论出来了:许多国家每天的支出都低于一美元。这些国家中大多数都位于亚洲和非洲,看来世界卫生组织应更关注这些国家。