---
# System prepended metadata

title: 透過API網路爬蟲

---

# 透過API網路爬蟲取得天氣
## 更新 Python
前往[python](https://https://www.python.org/)官網!
點選 Downloads 底下的 Download for Windows 的Python 3.12.1
![image](https://hackmd.io/_uploads/S1Sf39rL6.png)



儲存完畢後開啟檔案!
把 Add python.exe to PATH 打勾，然後點 Install Now
![image](https://hackmd.io/_uploads/HJG-3qrIa.png)




## 使用 VS code
打開 Visual Studio Code
在最左邊的那行找到四個小格子的 延伸模組 並查詢 python 並點 install
![image](https://hackmd.io/_uploads/Hkf9iqrLp.png)

找一個地方創等一下放Python檔案的資料夾(名字隨便) ex:桌面

回到 VS code
點選 file -> Add folder to Workplace
![image](https://hackmd.io/_uploads/ryjuTcr8T.png)

選取你剛剛創的資料夾
![image](https://hackmd.io/_uploads/r1LVAqHUp.png)

在你的資料夾底下創一個python檔(名字隨便，記得加 .py 就好)
![image](https://hackmd.io/_uploads/rJ7cRqBI6.png)

載入 module 
```python!
import urllib.request as requests
import json
```
![image](https://hackmd.io/_uploads/SJbiJsB86.png)

## 取得中央氣象局API
去 中央氣象局的 [氣象資料開放平台] 點 登入/註冊(https://opendata.cwb.gov.tw/index)
![image](https://hackmd.io/_uploads/ByDifiHIa.png)

點氣象會員登入(facebook的沒辦法用) 下面的 加入會員
![image](https://hackmd.io/_uploads/Bybe7oSUp.png)

自己輸入帳密
![image](https://hackmd.io/_uploads/rkHzXoSI6.png)

去gmail確認註冊
![image](https://hackmd.io/_uploads/rJUOQiSIp.png)

回來登入
![image](https://hackmd.io/_uploads/SyUj7srUT.png)

按取得授權碼
![image](https://hackmd.io/_uploads/BkIg4sBLp.png)

點資料主題的全部
![image](https://hackmd.io/_uploads/ryTfNjS8a.png)

查 36小時天氣預報
![image](https://hackmd.io/_uploads/SJyr4oBU6.png)

點進去 -> 選API
![image](https://hackmd.io/_uploads/SJjU4jBIp.png)

點 try it out
![image](https://hackmd.io/_uploads/By8qNoSIT.png)

在 Authorization 輸入剛剛的授權碼
![image](https://hackmd.io/_uploads/Sk28rsSLp.png)


把 location 定在高雄 (其他都不用管)
![image](https://hackmd.io/_uploads/S11kroBUa.png)

滑到下面點 Excute
![image](https://hackmd.io/_uploads/HkhgSirLT.png)

複製他給你的 url 
![image](https://hackmd.io/_uploads/S1OwLiSLp.png)

## 取得資料
回到 VS code 打入以下程式碼(我現場解釋)

```python=
with requests.urlopen('你剛剛複製的的url')as response:
       data_json = json.load(response)# 轉換成 JSON 格式
with open('weather.json', 'w', encoding='utf-8')as file:
    json.dump(data_json, file, ensure_ascii=False)
```
![image](https://hackmd.io/_uploads/rkNtLjBIp.png)

儲存後執行檔案
![image](https://hackmd.io/_uploads/r1CEwsrIT.png)

應該會看到你左邊有多出一個檔案叫 weather.json
![image](https://hackmd.io/_uploads/rke-dsHIT.png)

點進去後會看到一段很亂的東西
![image](https://hackmd.io/_uploads/HJQTOsHL6.png)

去VS code的設定
![image](https://hackmd.io/_uploads/By7xYiBIa.png)

輸入 format on save 並打勾
![image](https://hackmd.io/_uploads/rkEQtiH8p.png)

回weather.json 按 control + s (現場教學解讀檔案)
![image](https://hackmd.io/_uploads/B14IYjBL6.png)

## 最後的程式碼
以下是最後的程式碼

```python=
import urllib.request as requests
import json

with requests.urlopen('你剛剛複製的的url')as response:
       data_json = json.load(response)# 轉換成 JSON 格式
with open('weather.json', 'w', encoding='utf-8')as file:
    json.dump(data_json, file, ensure_ascii=False)

location = data_json['records']['location'][0]['weatherElement'][0]['time'][1]['parameter']['parameterName']
PoP = data_json['records']['location'][0]['weatherElement'][1]['time'][1]['parameter']['parameterName']
MinT = data_json['records']['location'][0]['weatherElement'][2]['time'][1]['parameter']['parameterName']
Ci = data_json['records']['location'][0]['weatherElement'][3]['time'][1]['parameter']['parameterName']
MaxT = data_json['records']['location'][0]['weatherElement'][4]['time'][1]['parameter']['parameterName']
print(f"天氣: {location}")
print(f"降雨率: {PoP}")
print(f"氣溫: {MinT} ~ {MaxT}")
print(f"舒適度: {Ci}")
```
![image](https://hackmd.io/_uploads/S1SH9jHLT.png)









