Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
Búsqueda de selector múltiple a través de la biblioteca de titiriteros

Uso titiritero para obtener datos sobre la tienda. Busco usando los p.shop-page-content__text_large, span.shop-list-item__address , pero me encontré con un problema tal que solo uno de ellos puede estar presente en la página. Traté de resolver el problema de la siguiente manera, pero no funciona. Dime, ¿cómo se puede arreglar esto?

 const puppeteer = require('puppeteer'); const browser = await puppeteer.launch({ headless: false, slowMo: 150, }); const cities = [{'CITY': 'Town1', 'LINK': '/shops/town1/'}, {'CITY': 'Town2', 'LINK': '/shops/town2/'}]; async function getData(page, selector) { return await page.$$eval(selector, info => info.map((data) => { let str = data.textContent.trim(), from = str.search(','), to = str.length; return { 'COUNTRY': 'unknow', 'STREET' : str.substring(from, to) } })); } const result = []; for (let val of cities) { console.log(val.LINK, val.CITY); const page = await browser.newPage(); await page.goto('https://www.example-site.ru' + val.LINK); data = await page.waitForFunction('.shop-page-content').then(async() => { console.log('ok'); return await getData(page, 'p.shop-page-content__text_large'); }).catch(async (e) => { console.log('fail'); await page.waitForSelector('.shops-info__section'); return await getData(page, 'span.shop-list-item__address'); // result.push(data); }); result.push(data); await browser.close(); } console.log(result);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Resultó así:

 const browser = await puppeteer.launch({ headless: false, slowMo: 150, }); const cities = [{'CITY': 'Town1', 'LINK': '/shops/town1/'}, {'CITY': 'Town2', 'LINK': '/shops/town2/'}]; const page = await browser.newPage(); const result = []; for (let val of cities) { await page.goto('https://www.example-site.ru' + val.LINK); const list = await page.evaluate(() => { const data = []; const elements = document.querySelectorAll('p.shop-page-content__text_large').length ? document.querySelectorAll('p.shop-page-content__text_large') : document.querySelectorAll('span.shop-list-item__address'); for (const element of elements) { data.push(element.innerText); } return data; }); result.push({ link: val.LINK, city: val.CITY, list }) } await browser.close();
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!