LoginSignup
0
1

noteのユーザー情報を取得ー覚え書き1

Last updated at Posted at 2023-09-15

WEBサービスnoteからユーザー情報を抽出して
コンソールで表示するJavascriptの覚え書き

検索窓からキーワードで検索
検索結果のページでユーザー情報を取得します

※TOPページでは機能しない
u3er298ur89u8fd3u8fwu32ur8f.png
【取得する情報】
・ユーザーネーム
・ユーザーID
・ユーザープロフィールのURL

// XPathの一般的なパターン
const xpathPattern = '//*[@id="__layout"]/div/div[1]/main/div[3]/div/div/div/div/div/div[2]/div/div/section/div/div[3]/div/div[1]/a';

// XPathの一般的なパターンに一致するすべての要素を取得
const elements = document.evaluate(xpathPattern, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

// 取得した要素に対して処理を繰り返す
for (let i = 0; i < elements.snapshotLength; i++) {
  const element = elements.snapshotItem(i);

  // 要素のテキストを表示
  console.log('要素のテキスト:', element.textContent);

  // 要素のURLを取得
  const url = element.getAttribute('href');
  console.log('要素のURL:', 'https://note.com' + url);

  // ユーザーIDの取得
  const userId = url.match(/\/([^/]+)\/?$/)[1];
  console.log('ユーザーID:', userId);
}

※メモ
これだけではあまり使い勝手の良い物では無い

0
1
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
1