Last active
December 7, 2018 03:06
-
-
Save baobao/6a5a6b15dbbb20589b0367de3049cf5d to your computer and use it in GitHub Desktop.
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 requests | |
import json | |
import sys | |
import os | |
API_KEY = "APIキー" | |
api = "http://api.openweathermap.org/data/2.5/weather?q={city}&APPID={key}" | |
city_name = sys.argv[1] | |
url = api.format(city = city_name, key = API_KEY) | |
print(url) | |
response = requests.get(url) | |
data = json.loads(response.text) | |
print(data) | |
today = datetime.datetime.now() | |
file_name = "weather/weather" + today.strftime("%Y-%m-%d-%H-%M") + ".json" | |
print(file_name) | |
# ディレクトリが存在しない場合のみ作成 | |
if os.path.exists("weather")==False: | |
os.mkdir("weather") | |
with open(file_name, "w", encoding="utf-8") as f: | |
f.write(response.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment