Home > Software engineering >  Fetching data from URL which loads slow - missing few html tags in response
Fetching data from URL which loads slow - missing few html tags in response

Time:12-27

I tried using fetch to pull sector information of a stock from a table on a web page using a below code.

fetch('https://www.nseindia.com/get-quotes/equity?symbol=3IINFOLTD')
      .then(res => res.text())
      .then((responseText) => {
        
        let document = new DOMParser().parseFromString(responseText, 'text/html');
        let table = document.getElementById('industryInfo');
        console.log(table);
});

however, the table tag is missing tbody with the above method, how do we wait until the entire page loads and the parse the document object from the response.

below is the table tag from the response

<table id="industryInfo" >
    <thead>
        <tr>
            <th>Macro-Economic Sector</th>
            <th>Sector</th>
            <th>Industry</th>
            <th>Basic Industry</th>
        </tr>
    </thead>
</table>

CodePudding user response:

The source code received by a GET request does not contain the tbody. That is generated by the JavaScript code on the page. You may want to instead do a fetch request to https://www.nseindia.com/api/quote-equity?symbol=3IINFOLTD.

fetch('https://www.nseindia.com/api/quote-equity?symbol=3IINFOLTD')
  .then(res => res.json())
  .then(json => {
    console.log(json.industryInfo.macro);
    console.log(json.industryInfo.sector);
    console.log(json.industryInfo.industry);
    console.log(json.industryInfo.basicIndustry);
});

// Outputs:
// "Information Technology"
// "Information Technology"
// "IT - Software"
// "Computers - Software & Consulting"
  • Related