1
const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    const url= 'https://www.booking.com/searchresults.en-us.html?label=gen173nr-1FCAEoggI46AdIM1gEaLUBiAEBmAExuAEXyAEP2AEB6AEB-AECiAIBqAIDuAL_6vKbBsACAdICJDA0MjU3ZGNjLTJlOWEtNDMyYi1hNDQ2LTg0MmIwMjczYjMzYtgCBeACAQ&sid=5727a604ee8453f41f52e9d9b2d7a5c8&aid=304142&ss=Mingora&ssne=Mingora&ssne_untouched=Mingora&efdco=1&lang=en-us&sb=1&src_elem=sb&src=searchresults&dest_id=-2769132&dest_type=city&checkin=2022-11-27&checkout=2022-11-30&group_adults=2&no_rooms=1&group_children=0&sb_travel_purpose=leisure'
    await page.goto(url);
    await page.screenshot({path: 'example.png'});
    const selector = "#search_results_table div > h3 > a";

    const titles = await page.evaluate(() => 
        Array.from(document.querySelectorAll("h3.a4225678b2 a.e13098a59f"))
            .map((name) => name.textContent)
        );
        
    console.log(titles.length);
    console.log(titles);
    await browser.close();
})();

Here is my simple code that I want to use to scrape names of the hotels in a particular location. The query works fine when I run it in the console in the Google Chrome browser but when I run it in Node.js with Puppeteer, it returns an empty array. What am I doing wrong?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Johnfranklien
  • 525
  • 3
  • 5
  • 15
  • 1
    Does this answer your question? [Why does headless need to be false for Puppeteer to work?](https://stackoverflow.com/questions/63818869/why-does-headless-need-to-be-false-for-puppeteer-to-work) – ggorlen Nov 23 '22 at 16:33
  • Looks like you're being detected as a bot when running headlessly. Either run headfully or [add a user-agent](https://stackoverflow.com/a/70936552/6243352) (or take more drastic action). – ggorlen Nov 23 '22 at 16:33

0 Answers0