[Python] Using Python to get weather data
Here's a simple Python code snippet for finding the weather.
1.
1.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import urllib.request
from contextlib import closing
import xml.etree.ElementTree as ET
rssurl = 'https://rss-weather.yahoo.co.jp/rss/days/4410.xml'
with closing(urllib.request.urlopen(rssurl)) as res:
xml = res.read()
tree = ET.fromstring(xml)
root = tree.findall('.')
for item in root[0].iter('item'):
title = item.find('title').text
print(title[:title.index(' - Y')])
2.
import requests
import json
def get_weather():
url = 'http://weather.livedoor.com/forecast/webservice/json/v1'
payload = {'city': '130010'}
data = requests.get(url, params = payload).json()
#resp = urllib2.urlopen('http://weather.livedoor.com/forecast/webservice/json/v1?city=%s'%citycode).read()
#resp = json.loads(resp)
print(data['description']['text'])
print(data['title'])
for weather in data['forecasts']:
print(weather['dateLabel'] + ':' + weather['telop'] + '(' + weather['date'] + ')')
if __name__ == '__main__':
get_weather()
Comments
Post a Comment