I am trying to create a datascraper using node.
Here is a sample html code for an item that I am trying to scrape:
JavaScript
x
5
1
<tr class="cool">
2
<td>Todd</td>
3
<td>Bob eats shoes <br/><a href="/cool/donkey" title="fluffy" class="stack">[Stack]</a>
4
</tr>
5
Here is some code that I am using to extract:
JavaScript
1
7
1
cars.forEach(carCard=> {
2
const carCool = {
3
number: carCard.querySelector('?').textContent,
4
date: carCard.querySelector('?').textContent,
5
};
6
});
7
I was wondering if there was anyway I could get the text of ‘Todd’ and [Stack] using this query selector. I do not know what I would need to put in place of the question marks. If not is there a different method I can use to accomplish this?
Please help.
Advertisement
Answer
You could do the following:
JavaScript
1
7
1
// To get all the td fields
2
const tds = document.querySelectorAll('td');
3
// to get the content of the td fields
4
tds.forEach(td => {
5
console.log(td.textContent);
6
})
7