0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

メルカリのカテゴリ名とURLを抽出するスクリプト

Posted at

カテゴリー情報の抽出

メルカリの「カテゴリーからさがす」ページから
項目とURLを抽出するスクリプト

URL:https://jp.mercari.com/categories

※大カテゴリ、小カテゴリ全てで使えます。

サンプルコード

function displayLinksInBrowser(html) {
  const tempElement = document.createElement('div');
  tempElement.innerHTML = html;

  const links = tempElement.querySelectorAll('.content__884ec505 a');

  const table = document.createElement('table');
  const tableBody = document.createElement('tbody');

  links.forEach(link => {
    const path = link.getAttribute('href');
    const url = `https://jp.mercari.com${path.replace(/\/search\?category_id=/, '/')}`;
    const linkText = link.innerText;

    const row = tableBody.insertRow();
    const textCell = row.insertCell();
    const urlCell = row.insertCell();

    textCell.appendChild(document.createTextNode(linkText));
    urlCell.appendChild(document.createTextNode(url));
  });

  table.appendChild(tableBody);

  const tableContainer = document.createElement('div');
  tableContainer.style.cssText = 'position:fixed;top:0;left:0;width:100vw;height:100vh;background-color:rgba(0, 0, 0, 0.7);display:flex;justify-content:center;align-items:center;';

  const innerContainer = document.createElement('div');
  innerContainer.style.cssText = 'padding:20px;border:1px solid #ccc;background-color:#000000;max-width:90%;';
  innerContainer.appendChild(table);
  tableContainer.appendChild(innerContainer);

  const closeButton = document.createElement('button');
  closeButton.textContent = '閉じる';
  closeButton.addEventListener('click', () => tableContainer.remove());
  innerContainer.appendChild(closeButton);

  document.body.appendChild(tableContainer);
}

const htmlContent = document.querySelector('.merList').outerHTML;
displayLinksInBrowser(htmlContent);

スクリーンショット

75c9810b38048b88eca239a25f6328a9.png

備忘録

サイトトップのメニューから入った場合の大カテゴリURL

 https://jp.mercari.com/s/カテゴリID

カテゴリー検索ページから入った場合の大カテゴリURL

 https://jp.mercari.com/search?category_id=カテゴリの番号

抽出されるURL

 https://jp.mercari.com/categories?category_id=カテゴリの番号

「categories?」のままだとカテゴリ検索ページのままなので
別システム等で使用する場合、「categories?」が「search?」に置き換える

 https://jp.mercari.com/search?category_id=カテゴリの番号

以上

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?