How to export this node.js function in react.js

i am working on a dashboard were the user should be able to click on a button and then get some data that would have been scraped from a site. I used puppeteer and it prints the desired data to the console but now how can i add this to my react js application ?

Here's the puppeteer code i wrote :

const puppeteer = require('puppeteer');

async function scrapeProduct(url) {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto(url);

    const [el] = await page.$x('/html/body/div[6]/div[3]/div/div[1]/div[1]/div/div/div[1]/a/img');
    const src = await el.getProperty('src');
    const srcTxt = await src.jsonValue();

    const [el2] = await page.$x('/html/body/div[6]/div[3]/div/div[1]/div[1]/div/div/div[2]/div/a/h3');
    const txt = await el2.getProperty('textContent');
    const rawTxt = await txt.jsonValue();

    const [el3] = await page.$x('/html/body/div[6]/div[3]/div/div[1]/div[1]/div/div/div[2]/div/div');
    const txt2 = await el3.getProperty('textContent');
    const tags = await txt2.jsonValue();

    console.log({srcTxt, rawTxt, tags});
    browser.close();
}

scrapeProduct('https://2degrees-investing.org/resources/');

Is there a way to export this function and use it as an import somewhere ? Thanks in advance.



Read more here: https://stackoverflow.com/questions/67935116/how-to-export-this-node-js-function-in-react-js

Content Attribution

This content was originally published by Silent observer at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: