LoginSignup
0
0

More than 1 year has passed since last update.

Chromeで、TableをCSVで取得(楽天証券積立設定取得)(自分向けメモ)

Posted at

楽天証券の積立設定を取得

複数ページにまたがる場合は、手動で切り替えてから取得。。。

var downloadAsTextFile = function(fileName, content) {
    var bom = new Uint8Array([0xEF, 0xBB, 0xBF]);
    var blob = new Blob([bom, content]);
    var url = window.URL || window.webkitURL;
    var blobURL = url.createObjectURL(blob);

    var a = document.createElement('a');
    a.download = fileName;
    a.href = blobURL;
    a.click();  
};
var downloadAsCSVFile = function(fileName, rows) {
  var content = "";
  for( var i in rows) {
    var ary = rows[i];
    for (var j = 0, m = ary.length; j < m; ++j) {
      content += '"' + ("" + ary[j]).replace('"', '""') + '"';
      if (j !== m) {
        content += ',';
      }
    }
    content += '\n';
  }
  downloadAsTextFile(fileName, content);
};
var Queue = function() {
  this.promise = Promise.resolve(true);
  this.async = (action)      => this.promise = this.promise.then(() => new Promise((resolve) => action(resolve)));
  this.sync  = (action)      => this.async((resolve) => { action(); resolve(); });
  this.delay = (delay)       => this.async((resolve) => setTimeout(resolve, delay));
  this.open  = (url, action) => this.async((resolve) => {
    var w = window.open(url, "dummy", 'width=100,height=100');
    $(w).load(() => { action(w); w.close(); resolve(); });
  });
};

var rows = []
var q = new Queue()
$("tr td > a[href*='/app/info']").each((i, e) => {
    var a = $(e)
    var tr = $(e).closest('tr')
    var td1 = tr.find('td:eq(0)').text().trim()
    var td3 = tr.find('td:eq(2)').text().trim()
    var td4 = tr.find('td:eq(3)').text().trim()
    var td5 = tr.find('td:eq(4)').text().trim()
    q.open(e.href, (w) => {
        
        var t = $(w.document)
        var isinCd = t.find("div.isinCd").first().text().trim()
        var type = t.find("span.fund-type").text().trim()
        var fee = t.find(".trust-fee td").text().trim()
        rows.push([a.text(), e.href, td1, td3, td4, td5, isinCd, type, fee])
    })
})
q.sync(() => downloadAsCSVFile("hoge.csv", rows))

ISINコードから協会コードに

楽天はISINで管理してるけど、SBIは協会コードで管理しているみたい。
対応表みたいのは見つからず、kakaku.com でISINから協会コードを引けるので、ここで変換。

Chromeで kakaku.com上でJavascriptを実行

Excelから楽天証券の積立設定のテーブルをコピペ。

tbl_org = `eMAXIS Slim 国内株式(TOPIX)	(...)	JP90C000ENA9
(...)
eMAXIS Slim 米国株式(S&P500)	(...)	JP90C000GKC6
`

ISINコードを取り出して、協会コードを抜き出し。
なお、jQueryが古いので、"on"がなく、"load"を使用。loadもversion 3からなくなるので面倒。。。

var q = new Queue();
var rows = [];
tbl = tbl_org.split("\n").map((l) => l.split("\t"))
tbl.forEach((row) => { 
    if(row.length <= 1) return
    console.log(row[6])
    var url = "https://kakaku.com/fund/detail.asp?si_isin=" + row[6]
    q.open(url, (w) => {
        var t = $(w.document)
        var code = t.find('.fundCode').text().replace(/.*:/, "")
        row.push(code)
        rows.push(row)
    })
})
q.sync(() => downloadAsCSVFile("hoge.csv", rows))
0
0
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
0
0