LoginSignup
13
11

More than 3 years have passed since last update.

Node.jsでTwitterのフォロワー数をまとめて調べる方法

Last updated at Posted at 2018-12-03

Node.jsでTwitterのフォロワー数をまとめて調べる方法。

必要な環境はNode.jsのみ。npm initコマンドを使って、初期化します。

npm init -y

puppeteerというスクレイピング用のモジュールをインストールします。

npm i puppeteer

index.jsとか適当なファイル名でJSを書きます。配列のところに、いろんな人のアカウント名を書けば、まとめて調べてくれます。

index.js
const puppeteer = require('puppeteer');

// ここに調べたい人のアカウント名を書いてね
const list = [
  'clockmaker',
  'tonkotsuboy_com',
];

async function start() {
  // Chroniumさん、こんにちは
  const browser = await puppeteer.launch();
  // 新しいタブを開いてね
  const page = await browser.newPage();

  for (let i = 0; i < list.length; i++) {
    const listElement = list[i];

    try {
      const url = 'https://twitter.com/' + listElement;

      // ページへ移動
      await page.goto(url);

      // 任意のJavaScriptを実行
      const count = await page.evaluate(() => {
        // フォロー数情報を抜き取る
        const stringCount = document
          .querySelector('li.ProfileNav-item--followers')
          .querySelector('.ProfileNav-value')
          .dataset.count;
        // 数値へ変換
        return Number(stringCount);
      });
      console.log(listElement, count);
    } catch (e) {
      console.error(e);
    }
  }

  // はい、終了〜
  browser.close();
}

start();

すると、コマンド入力をして、Node.jsを実行します。

node index.js

こんな感じでフォロワー数を取れます。

image.png

13
11
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
13
11