Extract binding of isaac item names

mail@pastecode.io avatar
unknown
javascript
a year ago
1.4 kB
1
Indexable
const axios = require('axios');
const cheerio = require('cheerio');
const fs = require('fs');

async function scrapeWebsite() {
  const url = 'https://bindingofisaacrebirth.fandom.com/wiki/Collection_Page_(Repentance)';
  try {
    const response = await axios.get(url);
    const $ = cheerio.load(response.data);

    const itemMapping = {};

    // Select the <td> elements that contain the items
    $('table.collection-page.wikitable tbody tr td').each((index, element) => {
      const span = $(element).find('span.tooltip');
      const img = $(element).find('img[alt*="Collectible"]');

      if (span.length > 0) {
        const entityID = index + 1; // Entity IDs start from 1
        let itemName = span.attr('data-tooltip').split('|')[1];

        if (img.length > 0) {
          const altText = img.attr('alt');
          const matches = altText.match(/Collectible\s(.+?)\sicon/);
          if (matches) {
            itemName = matches[1];
          }
        }

        itemMapping[entityID] = itemName;
      }
    });

    // Log the item mapping
    console.log(itemMapping);

    // Export itemMapping to a JavaScript file
    const jsCode = `const itemMapping = ${JSON.stringify(itemMapping, null, 2)};\n\nmodule.exports = itemMapping;`;

    fs.writeFileSync('itemMapping.js', jsCode);

    console.log('Item mapping exported to itemMapping.js');
  } catch (error) {
    console.error('Error:', error);
  }
}

scrapeWebsite();