[英]how to remove newline from data received in node js with puppeteer
我需要以下代碼的幫助。 如何刪除 rawTxt output 中的所有換行符 \n? rawTxt 的當前 output 是 rawTxt: '\n\n\n\n\n\n\n\nKangol Men, Women Washed Bucket Hat\n\n\n\n\n\n\n'
我試過 .replace(/\n/g) 和 .replace([\s\S]) 但沒有運氣。 今天一直在尋找,但沒有運氣:(
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' }
您可以嘗試以下代碼:
const input = '\n\n\n\n\n\n\n\nKangol Men, Women Washed Bucket Hat\n\n\n\n\n\n\n'; console.log('initial input=', input); console.log('new input =', input.replaceAll('\n',''));
看來您只是放錯了替換電話。 嘗試這個:
const [el2] = await page.$x('//*[@id="productTitle"]');
const txt = await el2.getProperty('textContent');
const rawTxt = (await txt.jsonValue()).replace(/\n/g, '');
或這個:
const [el2] = await page.$x('//*[@id="productTitle"]');
const txt = await el2.getProperty('textContent');
const rawTxt = (await txt.jsonValue()).trim();
或者這個( innerText
通常更具可讀性並且空格更少):
const [el2] = await page.$x('//*[@id="productTitle"]');
const txt = await el2.getProperty('innerText');
const rawTxt = (await txt.jsonValue());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.