JustPaste.it

Phantomscrape

(async function() {
    const data = [];
    const maxRetries = 5;
    let page = 1;
    const delay = ms => new Promise(resolve => setTimeout(resolve, ms));

    // Function to scrape data from the current page
    async function scrapePage() {
        return new Promise((resolve, reject) => {
            try {
                const tableRows = document.querySelectorAll('table tbody tr'); // Adjust if necessary
                if (tableRows.length === 0) {
                    resolve(0); // No rows found, resolve with 0
                    return;
                }
                const rows = Array.from(tableRows).map(row => {
                    const cells = row.querySelectorAll('td');
                    return Array.from(cells).map(cell => cell.textContent.trim());
                });
                data.push(...rows);
                resolve(rows.length);
            } catch (error) {
                reject(error);
            }
        });
    }

    // Function to go to the next page
    async function goToNextPage() {
        return new Promise((resolve, reject) => {
            try {
                const nextButton = document.querySelector('button[analyticsval1="goToNextPageLink"]'); // Adjust selector if necessary
                if (nextButton && !nextButton.hasAttribute('disabled')) {
                    nextButton.click();
                    resolve(true); // Successfully clicked next page
                } else {
                    resolve(false); // No next page or button is disabled
                }
            } catch (error) {
                reject(error);
            }
        });
    }

    // Main function to scrape all pages
    async function scrapeAllPages() {
        let retries = 0;
        let hasNextPage = true;

        while (hasNextPage) {
            try {
                console.log(`Scraping page ${page}...`);
                const rows = await scrapePage();
                if (rows === 0 && page === 1) break; // Stop if no rows on the first page
                page++;
                retries = 0;
                await delay(2000); // Wait before clicking next page
                hasNextPage = await goToNextPage();
                if (!hasNextPage) break; // Stop if no next page
                await delay(2000); // Wait for the next page to load
            } catch (error) {
                console.error('Error scraping page:', error);
                retries++;
                if (retries >= maxRetries) break; // Stop if too many retries
                await delay(5000); // Wait before retrying
            }
        }
    }

    await scrapeAllPages();

    // Save to file
    const csvContent = data.map(row => row.join(',')).join('\n');
    const blob = new Blob([csvContent], { type: 'text/csv' });
    const url = URL.createObjectURL(blob);
    const link = document.createElement('a');
    link.href = url;
    link.download = 'scraped_data.csv';
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);

    console.log(`Saved ${data.length} rows of data.`);
})();

////If you run into issues. Dm on Instagram @petriviktj