roberthsu2003 / __2024_07_01_Shilin_python__

士林python ai 初階
Apache License 2.0
13 stars 4 forks source link

1請下載新北市youbike2.0的json即時資料,並將各站點資料print出來,2請輸入行政區,輸出此行政區的所有站點資訊 #11

Open roberthsu2003 opened 1 month ago

roberthsu2003 commented 1 month ago
youbike_url = 'https://data.ntpc.gov.tw/api/datasets/010e5b15-3823-4b20-b401-b1cf000550c5/json?size=1000'
heychi9487 commented 1 month ago
import requests
from pprint import pprint
url='https://data.ntpc.gov.tw/api/datasets/010e5b15-3823-4b20-b401-b1cf000550c5/json?size=1000'
response=requests.get(url)

for i in range (1,1000,1):
    bruh:list[dict] = response.json()[i]  #response 是實體 .json是方法
    print(bruh)
a=input('請輸入新北市行政區')
for i in range (1,1000,1):
    bruh:list[dict] = response.json()[i]  #response 是實體 .json是方法
    if bruh['sarea']==a:
        print(bruh)
Bowei0204 commented 1 month ago
import requests
from pprint import pprint

youbike_url = 'https://data.ntpc.gov.tw/api/datasets/010e5b15-3823-4b20-b401-b1cf000550c5/json?size=1000'
response = requests.get(youbike_url)

data = response.json()

district = input("請輸入新北市行政區: ")
district = district + "區"

district_stations = []
for station in data:
    if station['sarea'] == district:
        district_stations.append(station)

if district_stations:
    pprint(district_stations)
else:
    print(f"沒有找到 {district} 行政區的站點資訊。請再輸入一次")
Bergess0212 commented 1 month ago
import requests
from pprint import pprint
import pyinputplus as pyip

youbike_url = 'https://data.ntpc.gov.tw/api/datasets/010e5b15-3823-4b20-b401-b1cf000550c5/json?size=1000'
response = requests.get(youbike_url)
records:list[dict] = response.json()
area = pyip.inputStr("請輸入行政區(輸入須打出區):")
print(area,"\n")

stations = []
for record in records:
    if record['sarea'] == area:
        stations.append(record)
try:
    if stations:
        pprint(stations)
    else:
        raise NameError("沒有這個區")
except Exception as error:
    print(error)
sp11055422 commented 1 month ago
from os.path import abspath,dirname,join
import json
from pprint import pprint
import requests # type: ignore

file_path=join(dirname(abspath(__name__)),'data','新北市公共自行車租賃系統(YouBike)_export.json')
file_path
with open(file_path,encoding='utf-8',newline='') as file:
    content=json.load(file)

url='https://data.ntpc.gov.tw/api/datasets/010e5b15-3823-4b20-b401-b1cf000550c5/json?size=1000' 
response=requests.get(url)

records= response.json()
h =input("請輸入行政區(區)")
man=[]
for recode in records: 
   if recode['sarea']==h:
      man.append(recode)
pprint(man)