跳到主要內容

Python 簡單的空間站目前的位置 API 呼叫

前言

    如果我們想從某個外部服務取得資料,那我們需要知道資料儲存在哪裡。藉由API 端點和 API 呼叫,可以讓我們取得想要資料。底下我們藉由一個簡單的 API 呼叫實作, 取得國際太空站目前位置,包括經度和緯度。

實作

    導入必要的函式庫: requests. requests 模組可讓您使用 Python 發送 HTTP 請求。HTTP 請求傳回一個包含所有回應資料(內容、編碼、狀態等)的回應物件。

代碼

import requests

api_url = "http://api.open-notify.org/iss-now.json"

response = requests.get(url=api_url)

if response.status_code == 200:
data = response.json()
latitude = float(data['iss_position']['latitude'])
longitude = float(data['iss_position']['longitude'])
print(f"空間站目前的緯度及經度: ({latitude}, {longitude})")
elif response.status_code == 404:
print("The requested resource was not found.")
else:
print("Failed to retrieve data from the API.")

註: 簡述 requests.Response() 回應物件 常用 屬性/方法描述

json() 傳回結果的 JSON 物件(如果結果以 JSON 格式寫入,否則會引發錯誤)。

raise_for_status() 如果發生錯誤,此方法傳回一個 HTTPError 物件。

reason 傳回與狀態碼對應的文字。

request 傳回請求此回應的請求物件。

status_code 傳回一個指示狀態的數字(200 表示正常,404 表示找不到)。

text 回傳回應的內容,採用 Unicode 編碼。

url 回傳回應的 URL。


執行:


查看地圖所在

https://www.latlong.net/Show-Latitude-Longitude.html



















進一步

    為了更好在世界地圖上標示經緯度 (latitude / longitude) 位置,我們將代碼加以修改進化。


導入必要的函式庫: Folium,Folium 是 Python 的地圖套件,互動式地圖、GPS 地圖、Marker標記、即時位置地圖

import folium

m = folium.Map(location=[latitude, longitude], zoom_start=1)

folium.Marker(
[latitude, longitude],
popup="ISS"
).add_to(m)

m.save("iss_map.html")

執行生成 iss_map.html,使用瀏覽器檢視












留言

這個網誌中的熱門文章

初探 Vue 呼叫 API 出現 CORS 跨來源資源共享 問題原因

提要:   在 {初探Vue 與 Spring boot 的對話} 專案 ,前端 Vue 應用程式 串接 後端 API 伺服器 ,axios 呼叫 API 時出現以下,”無法取得回應內容 (No 'Access-Control-Allow-Origin' header is present on the requested resource):” 錯誤訊息,根據查找相關資料 ,出現以下原因。 瀏覽器開發工具 錯誤訊息 畫面 錯誤原因: “ Access to XMLHttpRequest at ” from origin ‘http://localhost:8080’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource” 瀏覽器為了安全考量,實施了同源政策。 當您的前端應用程式 (http://localhost:8080) 嘗試呼叫一個不同來源 (不同協議、不同域名或不同埠號) 的 API 伺服器 (http://localhost:8088) 時,瀏覽器會主動阻止這個請求,除非伺服器明確地允許這個跨來源的存取。 同源政策限制(Same-Origin Policy): 同源政策限制了程式碼和不同網域資源間的互動,同源是指兩份網頁具備相同協定、埠號(如果有指定)以及主機位置 範例: 表列哪些 URL 與 URL http://www.example.com/api/p1 屬於同源: URL                                                   | 結果   | 原因 --------------------------------------------------------------------- http://www.example.com/api/p2     |...

初探 Vue 與 Spring boot 的對話之Frontend (Vue-Frontend)

  Front-end Vue 使用 REST API 建立 Vite 專案 可參考 { Vue 3 初探}  文章 danny@Danny-Yu projects % npm create vite@latest Need to install the following packages: create-vite@8.2.0 Ok to proceed? (y) y > npx > "create-vite" │ ◇   Project name: │   vue-frontend │ ◇   Select a framework: │   Vue │ ◇   Select a variant: │   TypeScript │ ◇   Use rolldown-vite (Experimental)?: │   No │ ◇   Install with npm and start now? │   Yes │ ◇   Scaffolding project in /Users/danny/Desktop/projects/vue-frontend... │ ◇   Installing dependencies with npm... added 47 packages, and audited 48 packages in 27s 6 packages are looking for funding   run `npm fund` for details found 0 vulnerabilities │ ◇   Starting dev server... > vue-frontend@0.0.0 dev > vite   VITE v7.2.4   ready in 411 ms   ➜   Local:   http://localhost:5173/   ➜   Network: use --host to expose   ➜   press h + enter to show...

初探 Spring 中的循環依賴

原因: 當兩個或多個 bean 直接或間接地相互依賴時, 就會出現 Circular Dependency (循環依賴) 如: Bean A -> Bean B -> Bean A import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class BeanA {          @Autowired     private BeanB beanB;     public String sayHi() {         return "Hi! 我是 Class A.";     } } import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class BeanB {          @Autowired     private BeanA beanA;     public String sayHi() {         return "Hi! 我是 Class B.";     } } 編譯時不會出現問題 danny@Danny-Yu demo % mvn clean install -Dmaven.test.skip=true                           ... ... [INFO] Installing /Users/danny/Desktop/projects/demo/target/dem...