Created
January 9, 2026 04:52
-
-
Save CookieBox26/66988df91df28c015cad05f932fe500a to your computer and use it in GitHub Desktop.
気象庁から名古屋と福岡の日時の気圧と気温をスクレイピングするスクリプト
This file contains hidden or 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
| from bs4 import BeautifulSoup | |
| from pathlib import Path | |
| import requests | |
| import numpy as np | |
| import pandas as pd | |
| import time | |
| class JMADataDaily: | |
| def __init__(self, prec_no=51, block_no=47636, year=2025, month=12, suffix='_nagoya'): | |
| cache_path = Path(( | |
| '~/cache/jma-daily_{}_{}_{}-{}.html' | |
| ).format(prec_no, block_no, year, str(month).zfill(2))).expanduser() | |
| if not cache_path.is_file(): | |
| print('Cache not found. Attempting to download...') | |
| time.sleep(1) | |
| url = ( | |
| 'https://www.data.jma.go.jp/stats/etrn/view/daily_s1.php' | |
| '?prec_no={}&block_no={}&year={}&month={}&day=&view=' | |
| ).format(prec_no, block_no, year, month) | |
| html = requests.get(url).text | |
| cache_path.write_text(html, newline='\n', encoding='utf8') | |
| print(f'Saved to {cache_path}.') | |
| else: | |
| html = cache_path.read_text(encoding='utf8') | |
| date_prefix = str(year) + '-' + str(month).zfill(2) + '-' | |
| soup = BeautifulSoup(html, 'html.parser') | |
| table = soup.find('table', id='tablefix1') | |
| rows = table.find_all('tr') | |
| data = [] | |
| for row in rows: | |
| cols = row.find_all('td') | |
| if len(cols) == 0: | |
| continue | |
| data.append([ | |
| date_prefix + cols[0].string.zfill(2), | |
| cols[1].string.strip().split()[0], | |
| cols[2].string.strip().split()[0], | |
| cols[6].string.strip().split()[0], | |
| cols[7].string.strip().split()[0], | |
| cols[8].string.strip().split()[0], | |
| ]) | |
| self.df = pd.DataFrame(np.array(data), columns=[ | |
| 'date', | |
| 'atmo_local' + suffix, | |
| 'atmo_sea' + suffix, | |
| 'temp_avg' + suffix, | |
| 'temp_high' + suffix, | |
| 'temp_low' + suffix, | |
| ]) | |
| @classmethod | |
| def get_months(cls, prec_no=51, block_no=47636, year=2025, months=[10, 11, 12], suffix='_nagoya'): | |
| df = cls(prec_no, block_no, year, month=months[0], suffix=suffix).df | |
| for month in months[1:]: | |
| df = pd.concat([df, cls(prec_no, block_no, year, month=month, suffix=suffix).df]) | |
| return df | |
| if __name__ == '__main__': | |
| df = JMADataDaily.get_months( | |
| prec_no=51, block_no=47636, year=2025, | |
| months=list(range(1, 13)), suffix='_nagoya', | |
| ) | |
| df_ = JMADataDaily.get_months( | |
| prec_no=82, block_no=47807, year=2025, | |
| months=list(range(1, 13)), suffix='_fukuoka', | |
| ) | |
| df = pd.concat([df, df_.iloc[:, 1:]], axis=1) | |
| df.to_csv('workspace/nazuna/tests/data/jma-daily_2025.csv', index=False, lineterminator='\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment