How can I simulate different user agents when scraping with Nightmare?

Nightmare is a high-level browser automation library for Node.js, which is commonly used for web scraping, among other things. To simulate different user agents when scraping with Nightmare, you can set the userAgent string for each new instance or for specific navigation commands within your scraping script.

Here's an example of how you can set a custom user agent in Nightmare:

const Nightmare = require('nightmare');
const nightmare = Nightmare();

const customUserAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3';

nightmare
  .userAgent(customUserAgent) // Set the custom user agent
  .goto('https://example.com') // Navigate to the website
  .evaluate(() => {
    // Extract the desired information
    return document.title;
  })
  .end()
  .then(title => {
    console.log(title); // Output the extracted information
  })
  .catch(error => {
    console.error('Error:', error);
  });

In the above code, the userAgent method is used to set a custom user agent string before navigating to the website with the goto method.

If you want to use different user agents for different requests within the same Nightmare instance, you can chain the userAgent calls before each goto like this:

const Nightmare = require('nightmare');
const nightmare = Nightmare();

const userAgents = [
  'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
  'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1'
];

function scrapeWithUserAgent(userAgent) {
  return nightmare
    .userAgent(userAgent)
    .goto('https://example.com')
    .evaluate(() => {
      return document.title;
    })
    .then(title => {
      console.log(`Title with user agent "${userAgent}":`, title);
    });
}

// Iterate over the user agents and perform the scraping with each one
(async () => {
  for (const userAgent of userAgents) {
    await scrapeWithUserAgent(userAgent);
  }
  await nightmare.end();
})().catch(error => {
  console.error('Error:', error);
});

In this second example, we create an async function that loops through an array of user agents and calls scrapeWithUserAgent for each one, which sets the user agent and performs the scraping operation.

Keep in mind that some websites may have defenses against scraping, and frequently changing user agents could be one factor that leads to your scraper being detected and potentially blocked. Always make sure to comply with the website's terms of service and use responsible scraping practices.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon