Reputation: 1575
Question 1:
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" class="eq-series tbl-securityinfo cap-hide">
<thead>
<tr>
<th>Macro-Economic Sector</th>
<th>Sector</th>
<th>Industry</th>
<th>Basic Industry</th>
</tr>
</thead>
</table>
Question 2:
While performing another task of similar nature, however, the website does not support JSON
. How do we get the date in this case?
I have tried using await
and setTimeOut(function() {}, 10000)
nothing works.
Upvotes: 1
Views: 106
Reputation: 160
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"
Upvotes: 1