LoginSignup
16
13

More than 5 years have passed since last update.

Webスクレイピングで電気料金を取得する(中部電力)

Last updated at Posted at 2017-11-17

電気料金を取得してLINEとかで通知したいです。
情報が以下のWebにしかないので止むを得ずスクレイピングします。

カテエネ(PC) | 中部電力が運営する家庭向けWEB会員サービス

1 モジュールの導入

必要なモジュールをラズパイにインストールします。
カテエネがjavascriptゴリゴリなのでseleniumに加えてphantomjsも使います。

sudo apt-get install python3-lxml
sudo apt-get install python3-cssselect

sudo pip3 install selenium
sudo pip3 install phantomjs

2 プログラム

以下の通りです。

sc.py
import lxml.html
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

#カテエネのサイトを開く
target_url = 'https://www.chuden.co.jp/home/index.html'
driver = webdriver.PhantomJS()
driver.get(target_url)

#ログインする
login_id = driver.find_element_by_id("loginId")
password = driver.find_element_by_id("password")
login_id.send_keys("ID")
password.send_keys("PW")
password.send_keys(Keys.RETURN)

#電気料金の照会ページへ移動
button = driver.find_elements_by_id("clubkatene-list-top-gn_data")
button[1].send_keys(Keys.CONTROL)
button[1].click()

#ページ下部、電気料金の表の値を取得
root = lxml.html.fromstring(driver.page_source)
date = root.cssselect("#tPastPerformanceTable th")
links = root.cssselect("#tPastPerformanceTable td")

#表示してみる
print("最新: "+date[6].text)
print(links[0].text)
print(links[5].text)
print(links[10].text)
print(links[15].text)
print(links[20].text)
print(links[25].text)
print(links[30].text)
print(links[35].text)
print(links[40].text)
print(links[45].text)
print(links[50].text)
print(links[45].text)
print(links[60].text)

#終了
driver.close()
driver.quit()

3 実行

結果:
スクリーンショット 2017-11-17 13.53.18.png

取れました。

4 終わりに

・意外と簡単にできる割りには楽しい
・lambdaに上げて定期実行して電気料金API化すると良さそう
・スクレイピングはサービス妨害になり得るので一日一回くらいで

16
13
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
16
13