title: run javascript code in node js using array of inputs
body: <p>This below code is used to scrape <code>long tail keywords</code> from google using node.js.
  the code is from <a href="https://github.com/datagrab/keyword-scraper" rel="nofollow noreferrer">https://github.com/datagrab/keyword-scraper</a></p>
  <p>it scrape one keyword at a time by running the code on terminal and save results on text file:</p>
  <pre><code>node scraper.js --keyword=&quot;amazon&quot; --limit=100 --file=&quot;amazon_keywords.txt&quot;
  </code></pre>
  <p>This is the code:</p>
  <pre><code>const puppeteer = require('puppeteer')
  const https = require('https')
  const fs = require('fs')
  const yargs = require('yargs/yargs')
  const {hideBin} = require('yargs/helpers')
  
  // Parse command-line arguments
  const argv = yargs(hideBin(process.argv)).argv
  
  const initialQuery = argv.keyword
  const outputFile = argv.file
  const maxPages = argv.limit
  
  if (!initialQuery || !maxPages || !outputFile) {
      return console.log('USAGE: node scraper.js --keyword=&lt;keyword&gt; --limit=&lt;page_limit&gt; --file=&lt;output_file&gt;')
  }
  
  const cookiePopupButton = '#introAgreeButton'
  const keywordSelector = '.card-section .nVcaUb &gt; a'
  
  const getSearchURL = (query) =&gt; `https://google.com/search?q=${encodeURIComponent(query)}&amp;lr=lang_en`
  
  const extractRelatedSearches = async (page) =&gt; {
      try {
          return await page.$$eval(keywordSelector, arr =&gt; arr.map(e =&gt; e.textContent.toLowerCase()))
      } catch (e) {
          console.log('Error extracting keywords.', e)
      }
  
      return []
  }
  
  const acceptCookiePopup = async (page) =&gt; {
      try {
          await page.waitForSelector(&quot;iframe&quot;, {timeout: 2000})
          const frameHandle = await page.$('iframe')
          const frame = await frameHandle.contentFrame()
          await frame.waitForSelector(cookiePopupButton, {timeout: 2000})
          await frame.click(cookiePopupButton)
      } catch(e) {
          console.log('Cookie consent button not shown.')
      }
  }
  
  const httpsAgent = new https.Agent({ keepAlive: true });
  
  (async () =&gt; {
      // Initialize Puppeteer and open a new page
      const browser = await puppeteer.launch({httpsAgent})
      const page = await browser.newPage()
  
      let pagesScraped = 0
      let query
  
      // We'll store the unique search terms in a Set
      const uniqueTerms = new Set()
      uniqueTerms.add(initialQuery)
      const requestQueue = []
  
      // Open our output file in append mode
      const stream = fs.createWriteStream(outputFile, {flags: 'a'})
  
      try {
          // Load the initial search page
          await page.goto(getSearchURL(initialQuery), { waitUntil: 'networkidle2' })
  
          // The cookie consent dialog will most probably pop up at the first search. So we'll find the accept button and click it.
          await acceptCookiePopup(page)
  
          do {
              // Extract the related search terms from the bottom of the page. Convert them to lower-case so we can detect duplicates more easily.
              const newTerms = await extractRelatedSearches(page)
  
              // For each new search extracted ...
              newTerms.filter(term =&gt; !uniqueTerms.has(term)).forEach(term =&gt; {
                  uniqueTerms.add(term)
  
                  // Write it in the output file
                  stream.write(term + '\n')
  
                  // Add it the to the request queue
                  requestQueue.push(term)
              })
  
              pagesScraped++
  
              // Dequeue the next term
              query = requestQueue.shift()
  
              if (query) {
                  console.log(`Performing search for '${query}'...`)
                  await page.goto(getSearchURL(query), { waitUntil: 'networkidle2' })
              }
          } while (query &amp;&amp; pagesScraped &lt; maxPages)
      } catch (e) {
          console.log('Error occurred when scraping page. ', e)
      }
  
      // Clean up things
      stream.close()
      await browser.close()
    })();
  </code></pre>
  <p>I want to run it for batch of keywords and it run with each keyword one by one, I mean the input is an array of keywords not just one and scrape each item in the array and save results for each item  in separate text file.</p>