Advertisement

TrimwebsolutionsTrimwebsolutions

Problem and Solutions

Extract src of <img> element (selected by class) from multiple HTML pages in node.js ?

By M.K. Verma · 2 May 2022

Extract src of <img> element (selected by class) from multiple HTML pages in node.js ?

To Extract src of <img> element (selected by class) from multiple HTML pages in node.js use the following steps:-

  1. Download each page in a loop with AJAX.
  2. Parse the DOM with something like jsdom.
  3. Use a selector with [querySelectorAll()].(https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll) to get each image element.
  4. Use a regular expression on the image src-attribute to get the robot name. Like: $img.src.match(/([^\/]+).png$/i)[1].
  5. Download all the robots with AJAX.
  6. Combine robot name and downloaded robot to an object with key value pairs.

Let me know if you need more help or a code example.