scrape tboi item+id

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

async function scrapeWebsite() {
  const url = 'https://tboi.com/all-items';
  try {
    const response = await axios.get(url);
    const $ = cheerio.load(response.data);

    const itemMapping = {};

    // Select the <li> elements that contain items with "ItemID"
    $('div.main div.allitems li').each((index, element) => {
      const itemID = $(element).find('p.r-itemid').text();
      const itemTitle = $(element).find('.item-title').text();

      if (itemID.includes('ItemID')) {
        const entityID = itemID.replace('ItemID:', '').trim();
        itemMapping[entityID] = itemTitle;
      }
    });

    // 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);
  } catch (error) {
    console.error('Error:', error);
  }
}

scrapeWebsite();