Fetch the value from html response in node js

Very new to nodejs didn't find similar post. I am trying to fetch data-clp-course-id from the html response got from the below code,

const https = require('https')
const options = {
    hostname: 'www.udemy.com',
    path: '/course/az-303-microsoft-azure-architect-practice-test-2020/',
    method: 'GET'
  const req = https.request(options, res => {
    console.log(`statusCode: ${res.statusCode}`)
    res.on('data', d => {
     // process.stdout.write(d)
  req.on('error', error => {

Sample html Response (given only the part of the response):

<!DOCTYPE html>
<html lang="en-us">

    <meta name="medium" content="mult">
    <meta name="title" content="AZ-303: Microsoft Azure Architect Practice Test 2020">
    <meta property="fb:app_id" content="313137469260">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta property="udemy_com:available" content="1">
    <meta property="udemy_com:category" content="IT &amp; Software">
    <meta property="udemy_com:instructor" content="https://www.udemy.com/user/fariha-rubab2/">
    <meta property="udemy_com:price" content="₹1,280">
    <meta property="og:title" content="AZ-303: Microsoft Azure Architect Practice Test 2020">
    <meta property="og:url" content="https://www.udemy.com/course/az-303-microsoft-azure-architect-practice-test-2020/">
        // Polyfill for IE in order to fix errors related with new Event() and new CustomEvent()
    // https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent#Polyfill
    (function () {
        if ( typeof window.CustomEvent === "function" ) return false;
        function CustomEvent ( event, params ) {
            params = params || { bubbles: false, cancelable: false, detail: undefined };
            var evt = document.createEvent( 'CustomEvent' );
            evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
            return evt;
        CustomEvent.prototype = window.Event.prototype;
        window.CustomEvent = CustomEvent;
        window.Event = CustomEvent;

<body id="udemy" class="
    ud-app-loader ud-component--course-landing-page-udlite
  udemy " data-clp-course-id="3523784" data-module-id="course-landing-page/udlite"

Any help how to fetch the data-clp-course-id value from the html response ?

1 answer

  • answered 2020-10-16 06:08 JYOTI PRAKASH MALLICK

    Just using Mike's suggestion

    async function getCourseCode() {
        try {
            let response = await fetch('https://www.udemy.com/course/az-303-microsoft-azure-architect-practice-test-2020/');
            let body = await response.text();
            let responseBody = body.match(/data-clp-course-id="(\d+)"/)
            let courseID = responseBody[1];