Type()という関数が活躍した。これで出力してきたデータの型がなんなのか分かるようになった。Jsonfileでロードしてきた情報はType:Noneなので、str()で変換しないと文字を連結できないということに気付かされた。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('***********') | |
No Comment to " Python3でLivedoor 天気APIを軽くたたいてみた "