LoginSignup
0
0

More than 1 year has passed since last update.

Chromeで、TableをCSVで取得(実質コスト取得)(自分向けメモ)

Posted at

実質コストをCSVで取得

毎度のライブラリ

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(); });
  });
};

CSVで取得

var rows = []
$('td:contains("■"):contains("インデックスファンド信託報酬ランキング")').each((i,e) => {
    e = $(e)
    var name = e.text()
    console.log(name)
    $('a:has(font)', e.closest('table')).each((j,f) => {
        var td = $(f).closest('td')
        var rank = td.prev('td').text().replace(/\s+/g, "")
        var fund1 = $('br', td).prev().text()
        var fund2 = $('br', td).next().text()
        var fee = td.next('td');
        var cost = fee.next('td')
        var assets = cost.next('td')
        fee = fee.text().replace(/\s+/g, "").replace(/(※\d+)/, "")
        cost = cost.text().replace(/\s+/g, "").replace(/(※\d+)/, "")
        assets = assets.text().replace(/\s+/g, "")
        var row = [name, rank, fund1, fund2, fee, cost, assets, f.href]
        console.log(row)
        rows.push(row)
        
    })
    
})
downloadAsCSVFile('hoge.csv', rows)

SBIのページで詳細を取得

ドメインが違うので、一旦CSVを取得した後、SBIのサイトで、詳細情報を取得

// CSVの中身をExcelで開き、テキストでは貼り付け

tbl_org = `■全世界株式 インデックスファンド信託報酬ランキング	1位	SBIインデックス	全世界株式	0.11%	0.12%	566.75	https://diamond.jp/zai/oo/cc/sbi_f_552
■全世界株式 インデックスファンド信託報酬ランキング	2位	eMAXIS Slim	全世界株式(オールカントリー)	0.11%	0.17%	5014.09	https://diamond.jp/zai/oo/cc/sbi_f_644
(中略)
■先進国リート インデックスファンド信託報酬ランキング	9位	eMAXIS	先進国リート	0.60%	0.72%	152.37	https://diamond.jp/zai/oo/cc/sbi_f_237
`

SBIのサイトでも同じようにライブラリを貼り付け、そのあと下記を実行。

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[7])
    q.open(row[7], (w) => {
        var t = $(w.document)
        var fund1    = t.find('div.md-l-utl-mt10 h3').first().text().trim()
        var fund2    = t.find('div.md-l-utl-mt10 h3').first().next('h3').text().trim()
        var category = t.find("p:contains('モーニングスターカテゴリ')").closest("p").next("p").text().trim()
        var code     = t.find("p:contains('協会コード')").closest("tr").next("tr").text().trim()
        var cost     = t.find("p:contains('信託報酬')").closest("tr").next("tr").text().trim()
        row.push(fund1, fund2, category, code, cost)
        console.log(row)
        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