Skip to content

Instantly share code, notes, and snippets.

@aoirint
Created August 3, 2018 11:26
Show Gist options
  • Save aoirint/f48958b50f819361d15a12f4706e38f2 to your computer and use it in GitHub Desktop.
Save aoirint/f48958b50f819361d15a12f4706e38f2 to your computer and use it in GitHub Desktop.
import requests
from bs4 import BeautifulSoup
def get_amedas(url):
r = requests.get(url)
bs = BeautifulSoup(r.content, 'html.parser')
main = bs.find('div', id="main_table")
table = main.find('table', id='tbl_list')
data = [ [ td.text for td in tr.find_all('td') ] for tr in table.find_all('tr') ]
for tr in data[2:]:
for i in range(len(tr)):
d = tr[i]
if d == '\xa0':
d = None
elif i in [ 0, 6, ]:
d = int(tr[i])
elif i in [ 1, 2, 4, 5, 7, ]:
d = float(tr[i])
tr[i] = d
#hour, temp, rain, winddir, windvel, suntime, humid, pres = tr
return data
def get_amedas_today(locId=44132):
url = 'http://www.jma.go.jp/jp/amedas_h/today-%d.html' % locId
return get_amedas(url)
def get_amedas_yesterday(locId=44132):
url = 'http://www.jma.go.jp/jp/amedas_h/yesterday-%d.html' % locId
return get_amedas(url)
if __name__ == '__main__':
td = get_amedas_today()
yd = get_amedas_yesterday()
print('Today: ')
print(td)
print()
print('Yesterday: ')
print(yd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment