.
Tags:

Python3でLivedoor 天気APIを軽くたたいてみた

By 文系なArduino 2017年7月16日日曜日
Type()という関数が活躍した。これで出力してきたデータの型がなんなのか分かるようになった。Jsonfileでロードしてきた情報はType:Noneなので、str()で変換しないと文字を連結できないということに気付かされた。




import urllib.request
import json
location = u'080020' #場所
url = 'http://weather.livedoor.com/forecast/webservice/json/v1?city=%s' %location
html = urllib.request.urlopen(url)
jsonfile = json.loads(html.read().decode('utf-8'))
print('***********')
print(jsonfile['pinpointLocations'][0]['name'])
print(jsonfile['forecasts'][0]['telop'])
#jsonfileでロードしてきた情報はType:Noneなので、str()で変換が必要
temp = str(jsonfile['forecasts'][0]['temperature']['max'])
if temp == u'None':
print('気温情報はありません')
else:
print('最高気温:' + str(jsonfile['forecasts'][0]['temperature']['max']['celsius']+'度'))
print('***********')
view raw gistfile1.txt hosted with ❤ by GitHub

Post Tags:

文系なArduino

Aduinoとraspberry PiとWebサービスで食べている「文系」です。マレーシアとシンガポールを拠点としてITコンサルタントとしても活躍し、中国語教師としても活躍中の身でもあります。IT農業をシンガポールで始めようと計画中です。

No Comment to " Python3でLivedoor 天気APIを軽くたたいてみた "