LoginSignup
1
2

More than 1 year has passed since last update.

石手川ダムリアルタイムデータをスクレイピングして貯水率を調べる

Last updated at Posted at 2022-06-29

石手川ダムリアルタイムデータ
http://www1.river.go.jp/cgi-bin/DspDamData.exe?ID=1368080150020&KIND=3&PAGE=0

from urllib.parse import urljoin

import pandas as pd
import requests
from bs4 import BeautifulSoup

def fetch_soup(url, parser="html.parser"):

    r = requests.get(url)
    r.raise_for_status()

    soup = BeautifulSoup(r.content, parser)

    return soup


url = "http://www1.river.go.jp/cgi-bin/DspDamData.exe?ID=1368080150020&KIND=3&PAGE=0"

soup = fetch_soup(url)

link = urljoin(url, soup.select_one("body > center > p > a").get("href"))


def dateparse(s):
    date, time = s.split()
    result = pd.to_datetime(date) + pd.to_timedelta(time + ":00")
    return result


df = pd.read_csv(
    link,
    skiprows=9,
    encoding="cp932",
    names=[
        "年月日",
        "時刻",
        "流域平均雨量",
        "流域平均雨量属性",
        "貯水量",
        "貯水量属性",
        "流入量",
        "流入量属性",
        "放流量",
        "放流量属性",
        "貯水率",
        "貯水率属性",
    ],
    parse_dates={"日時": ["年月日", "時刻"]},
    date_parser=dateparse,
    index_col=["日時"],
)

df
pd.options.plotting.backend = "plotly"

df["貯水率"].plot()

newplot.png

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2