fix the get berat javascript
Thu Nov 03 2022 01:34:23 GMT+0000 (UTC)
Saved by
@fiko942
#nodejs
#javascript
const puppeteer = require('puppeteer-extra')
const pluginStealth = require('puppeteer-extra-plugin-stealth')
puppeteer.use(pluginStealth())
async function main() {
const browser = await puppeteer.launch({
headless: false,
defaultViewport: null,
executablePath: 'C:\\c-dev\\chrome.exe'
})
const [page] = await browser.pages()
await page.goto('https://www.monotaro.id/s000009132.html', {timeout: 0, waitUntil: 'domcontentloaded'})
await page.waitForSelector('#product-attribute-specs-table', {timeout: 0})
const berat = await page.evaluate(() => {
let b = 0
const selector_head = '#product-attribute-specs-table tbody tr:nth-child({}) th'
const selector_body = '#product-attribute-specs-table tbody tr:nth-child({}) td'
let i = 1
document.querySelectorAll('#product-attribute-specs-table tbody tr').forEach(el => {
const data = {
name: document.querySelector(selector_head.replace('{}', i)).innerText,
value: document.querySelector(selector_body.replace('{}', i)).innerText
}
if(data.name == 'Berat (Kg)') b = data.value.match(/^[0-9]*\.?[0-9]*$/)[0]
i+=1
})
return b
})
console.log(berat)
}
main()
content_copyCOPY
Comments