def get_location(self, ip): try: geo_info = self.getGeoIPCode(ip).split(",") except: geo_info = ['', '', '', '', '', '', '', '', '', ''] loc = Location() loc.cc = geo_info[2] loc.cn = geo_info[3] loc.rc = geo_info[4] loc.rn = geo_info[5] loc.ct = geo_info[6] loc.zc = geo_info[7] loc.ll = geo_info[8] + "," + geo_info[9] return loc
url = 'https://opendata.cwb.gov.tw/api/v1/rest/datastore/O-A0003-001?Authorization=rdec-key-123-45678-011121314' html_content = requests.get(url) json_content = html_content.json() #print(json_content) records = json_content.get('records') location = records.get('location') #print(location) #for i in range(len(location)): # print(location[i]) allLocations = [] for item in location: l = Location() l.from_json(item) weather_element_json = item.get('weatherElement') element = WeatherElement() element.from_json(weather_element_json) l.weather_element = element allLocations.append(l) ''' locations = [] for l in location: location_site = Location() location_site.from_json(l) locations.append(location_site)
import requests from data import Location url = 'https://opendata.cwb.gov.tw/api/v1/rest/datastore/O-A0003-001?Authorization=rdec-key-123-45678-011121314' html_content = requests.get(url) #向html提出get要求 josn_content = html_content.json() #轉成json # print(html_content.text) records = josn_content.get("records") location = records.get('location') # print(location) allLocations = [] for item in location: l = Location() l.from_json(item) allLocations.append(l) print(l.__dict__) print(allLocations) #取得觀測資料 # weatherElement = item.get('weatherElement') # for element in weatherElement: # elementName = element.get('elementName') # elementValue = element.get('elementValue') # print(elementName,elementValue)
# print(location) # location為一個list (網址第45行 https://jsoneditoronline.org/#right=local.juropo&left=local.laripu # 取得欄位資料 '''for i in range(len(location)): #print出location 第一種方法 print(location[i]) ''' locations = [] # list for item in location: # print(item) => print出location 的第二種方法 # l = Location() # Location類別 # l.from_json(item) # 呼叫data類別裡的from_jason方法 # allocations.append(l) # print(l.__dict__) # print(allocations) # lat = item.get('lat') # lon = item.get('lon') # locationName = item.get('locationName') # stationId = item.get('stationId') # time = item.get('time') # obsTime = time.get('obsTime') # print(lat, lon, locationName, locationName, stationId, obsTime) location_site = Location() location_site.from_json(item) locations.append(location_site) # 取得觀測資料 weatherElement = item.get('weatherElement') for element in weatherElement: elementName = element.get('elementName') elementValue = element.get('elementValue') print(elementName, elementValue)
import requests #直接用alt+enter安裝套件 from data import Location, WeatherElement url = 'https://opendata.cwb.gov.tw/api/v1/rest/datastore/O-A0003-001?Authorization=rdec-key-123-45678-011121314' html_content = requests.get(url) #向html提出get請求 json_content = html_content.json() #將內容轉為json print(json_content) #取得測站資料 records = json_content.get('records') location = records.get('location') #從location裡面拿取資料 #取得欄位資料,location是list,解析這個資料 allLocactions = [] for l in location: #跑location裡面的資料,會得出dictionary資料 a = Location() a.from_json(l) weather_element_json = l.get('weatherElement') element = WeatherElement() element.from_json(weather_element_json) a.weather_element = element allLocactions.append(a) #lat = l.get('lat') #lon = l.get('lon') #locationName = l.get('locationName') #stationId = l.get('stationId') #time = l.get('time').get('obsTime') #print(locationName, stationId, time)
import requests from data import Location, WeatherElement url = 'https://opendata.cwb.gov.tw/api/v1/rest/datastore/O-A0003-001?Authorization=rdec-key-123-45678-011121314' html_content = requests.get(url) json_content = html_content.json() records = json_content.get('records') location = records.get('location') locations = [] for l in location: print(l) location_site = Location() location_site.from_json(l) locations.append(location_site)