NET/www/webプログラミング
の編集
https://over.6pb.info/wiki/?&8694a1ee7e
[
トップ
] [
編集
|
差分
|
履歴
|
添付
|
リロード
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
-- 雛形とするページ --
(no template pages)
***新電力 あしたでんき [#add6d35d] スマートメーターの情報をグラフで表示するページを電力各社は提供している。私が契約している「あしたでんき」は、データをCSV等でダウンロードできないので、画面から取得している。当初、Greasemonkeyで試したが失敗したので、Puppeteerに切り替えた。 このサンプルでは、前月のデータをTSVで書き出している。当月を取得したいなら前月に移動するコードを削除すればよい。 #pre{{ const puppeteer = require('puppeteer'); const fs = require('fs'); const cheerio = require('cheerio'); const path = require('path'); let tsvfile = path.join(__dirname, 'ashitadenki.tsv'); (async() => { const browser = await puppeteer.launch({ headless: false, args: [ '--no-sandbox', ] }); const page = await browser.newPage(); await page.setViewport({width: 1024, height: 2048}); try { await page.goto('https://ashita-denki.jp/login/', { waitUntil: 'networkidle2'}); await page.waitForSelector('input#username', {waitUntil: 'networkidle2', timeout: 10000}); await page.focus("input#username"); await page.type("input#username", 'your ID'); await page.focus("input#password"); await page.type("input#password", 'your password'); await page.waitFor(1000); await page.click('input[type=submit]'); await page.waitForNavigation({waitUntil: 'load'}); await page.waitForSelector('#monthly-report-table', {waitUntil: 'networkidle2', timeout: 2500}); var now = new Date(); var month = now.getMonth(); //1月は0 // 前の月を表示する await page.waitForSelector('#month > form > div:nth-child(1) > select:nth-child(2)'); await page.select('#month > form > div:nth-child(1) > select:nth-child(2)', month.toFixed()); await page.waitFor(3000); await page.waitForSelector('#monthly-report-table', {waitUntil: 'networkidle2', timeout: 2500}); let html = await page.$eval('#monthly-report-table > ul', e => e.outerHTML); $ = cheerio.load(html); let lines = []; $('li').each(function(){ let row = []; // #monthly-report-table > ul > li:nth-child(1) > time row.date = $(this).find('time').text().trim(); if (row.date == '') { return true; //$().eachのnext } row.date = row.date.replace(/日/,''); row.date = month.toFixed() + '/' + row.date; // #monthly-report-table > ul > li:nth-child(1) > ul > li.kwh > span row.kw = $(this).find('ul > li.kwh > span').text().trim(); // #monthly-report-table > ul > li:nth-child(1) > ul > li.cost > span row.cost = $(this).find('ul > li.cost > span').text().trim(); lines.push(Object.values(row).join("\t")); }); fs.writeFile(tsvfile, lines.join("\n"),(err) => { if (err) throw err; }); } catch (e) { console.log('caught'); process.stderr.write(e.toString()); await browser.close(); process.exit(1); } await browser.close(); })(); }}
タイムスタンプを変更しない
___paraedit_taxtarea___
テキスト整形のルールを表示する