2021年1月6日星期三

how to remove newline from data received in node js with puppeteer

I need help with the below code. How do I remove all the newline \n in the rawTxt output? The current output for rawTxt is rawTxt: '\n\n\n\n\n\n\n\nKangol Men, Women Washed Bucket Hat\n\n\n\n\n\n\n'

I tried .replace(/\n/g) and .replace([\s\S]) but no luck. Been searching around all day today but no luck :(

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('//*[@id="landingImage"]');      const src = await el.getProperty('src');      const srcTxt = await src.jsonValue();        const [el2] = await page.$x('//*[@id="productTitle"]'.replace(/\n/g, ''));      const txt = await el2.getProperty('textContent');      const rawTxt = await txt.jsonValue();        console.log({srcTxt, rawTxt});        browser.close();  }    scrapeProduct("https://www.amazon.com/Kangol-Mens-Washed-Cotton-Bucket/dp/B0758LZQW6/ref=sr_1_17?dchild=1&keywords=bucket+hat&qid=1609973810&sr=8-17");  

// { srcTxt: 'https://images-na.ssl-images-amazon.com/images/I/41TF7rSBy8L._AC_UX342_.jpg', rawTxt: '\n\n\n\n\n\n\n\nKangol Men, Women Washed Bucket Hat\n\n\n\n\n\n\n' }

https://stackoverflow.com/questions/65604662/how-to-remove-newline-from-data-received-in-node-js-with-puppeteer January 07, 2021 at 07:28AM

没有评论:

发表评论