LoginSignup
0
0

More than 1 year has passed since last update.

Chromeで、aタグのURLとテキストをCSVで取得(自分用メモ)

Last updated at Posted at 2022-05-21

jQueryの読み込み

jQueryのバージョンはコピペなので古い。。。

    if(typeof jQuery === "undefined") {
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = '//code.jquery.com/jquery-3.5.1.min.js';
        document.head.appendChild(script);
    }

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).on('load', () => { action(w); w.close(); resolve(); });
  });
};

aタグ取得

tbl = $('div#divSettingList tr td div a').map((i, e) => [[e.text, e.href]]).get()
downloadAsCSVFile('hoge.csv', tbl)

おまけ

SBIの積立設定一覧から項目を取得。
前も作った気がするけど、ページ構成が変わって使えない・・・

var q = new Queue();
var rows = [];
$('div#divSettingList tr td div a').each((i, e) => {
    var name = $(e).text()
    var amount = $(e).closest('td').find("p:contains('設定金額')").text();
    q.open(e.href, (w) => {
        var t = $(w.document);
        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()
        var row = [name, amount, category, code, cost]
        rows.push(row);
        console.log(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