推薦閱讀對象:

  1.  對於網路爬蟲有興趣的人
  2.  了解 json 的資料格式
  3.  應用 maps.world 製作簡易功能


我的閱讀動機:

  1.  網路時代帶來了數據爆炸的年代, 現在資料越來越多, 想要透過爬蟲的技術來幫助自己, 整理網路上的數據
  2.  想透過爬文的技術來做股市相關的分析
  3.  從新手開始, 學習Python 爬蟲

 

概述:

使用 json 資料, 整理人口資訊, 並應用 pygal.maps.world lib 繪製世界地圖

 

重點整理:

整理世界人口數據, 使用 pygal.maps.world

json 人口數據範例:

[{"Country Name": "World", "Country Code": "WLD", "Year": "2000", "Numbers": "6117806174.56156"},
 {"Country Name": "World", "Country Code": "WLD", "Year": "2010", "Numbers": "6894595189.85751"},
 {"Country Name": "Afghanistan", "Country Code": "AFG", "Year": "2000", "Numbers": "25951672.5"},
 {"Country Name": "Afghanistan", "Country Code": "AFG", "Year": "2010", "Numbers": "34385849.0"},
 {"Country Name": "Australia", "Country Code": "AUS", "Year": "2010", "Numbers": "22299153.0"},
 {"Country Name": "Australia", "Country Code": "AUS", "Year": "2000", "Numbers": "19153581.0"},
 {"Country Name": "China", "Country Code": "CHN", "Year": "2000", "Numbers": "1262645259.0"},
 {"Country Name": "China", "Country Code": "CHN", "Year": "2010", "Numbers": "1338300543.0"},
 {"Country Name": "Japan", "Country Code": "JPN", "Year": "2000", "Numbers": "126870782.0"},
 {"Country Name": "Japan", "Country Code": "JPN", "Year": "2010", "Numbers": "127451296.0"}
 ]

篩選 2021 年的人口數據:

載入人口數據檔案, 並挑出 2000年的資料列印

image

output:

image

 

認識 pygal_maps_world 的國碼資訊:

pygal_maps_world 最主要的功能是繪製世界地圖, 在使用前記得先安裝 package

pip install pygal_maps_world

 

先來認識 COUNTRIES, 這是一個 dict, key = 國家代碼, value = 國家名稱

接下來的繪圖會使用到國家代碼, 因此先讓大家了解這個結構

image

output:

image

 

繪製世界地圖

基本地圖繪製

範例, 標示中國地圖

image

output:

image

 

群組標記繪製

使用 add 函式時, 設定要為群集的 list

示範, 如何將國將設為群組, 並標記

image

output:

image

 

讓地圖呈現數據

將要標記的資料使用 add 函式, 第二個參數由 list 改為 dict, (key=country code, value=unmber)

輸出後, 將滑鼠移到特定位置, 就會顯示該地區對應的數字, 而該地區數字最大的顏色較深, 其餘一樣淺

image

 

output:

image

 

以人口量為分類顯示

了解如何使用 pygal_maps_world.maps.World() 後, 我們應用文章開頭的人口資料

- 取用 2000 年的資料

- 將資料以人口數做區分, 100000000 以上與未達 100000000

- 輸出人口地圖

image

image

image

 

output:

image

 

 

本日課程結束, 謝謝閱讀

Reference:

 

arrow
arrow
    文章標籤
    maps.world python 爬蟲
    全站熱搜
    創作者介紹
    創作者 JoeReader 的頭像
    JoeReader

    JoeReader的部落格

    JoeReader 發表在 痞客邦 留言(0) 人氣()