LoginSignup
1
2

More than 5 years have passed since last update.

ブラウザからツイートをJsonで取得する

Posted at

Twitterのタイムラインなどを開いて、以下のコードをConsoleから実行するとJsonで返るはず。
DOM構造が変わったら動かなくなる。
Chromeで確認したけど他のブラウザは不明。

var result = [];
var tweets = document.getElementsByClassName('stream-item');
var i = 0;
for (t of tweets) {
    if (t.childElementCount > 0 &&
        t.children[0].childElementCount > 1 &&
        t.children[0].children[1].children[1].childElementCount > 0) {

        var d = new Object();
        var h = t.children[0].children[1].children[0];
        d.name = h.children[0].children[1].children[0].innerHTML;
        d.screenName = h.children[0].children[2].innerText;
        d.time = h.children[1].children[0].title;
        d.url = h.children[1].children[0].href;
        d.text = t.children[0].children[1].children[1].children[0].innerText.replace(/\r?\n/g, " ");
        result[i++] = d;
    }
}
JSON.stringify(result);
1
2
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
1
2