In this website
https://www.worldometers.info/coronavirus/
I use this javascript function to know the position of the country in the table
{
function findMatchingRow(word) {
const found = []
const trList = document.querySelectorAll('#main_table_countries_today > tbody > tr')
trList.forEach((tr, i) => {
if (tr.textContent.match(word)) {
found.push({
index: i,
content: tr.textContent
})
}
});
return found
}
const matches = findMatchingRow("Australia")
console.log(matches)
if (matches.length > 0) {
console.log('found at:', matches.map(m => m.index))
}
}
For only Australia, it returns 8 instead of 35
for other countries like poland it gives correct number,
I still can't figure it out
Any help will be appreciated !
You do not have to get text content from the entire row, You can just match first td
content. There are places like Australia in anywhere in tr
. So narrow down the search.
function findMatchingRow(word) {
const trList = [...document.querySelectorAll(
"#main_table_countries_today > tbody > tr"
)];
let found;
trList.some((tr, i) => {
const name = tr.children[0].textContent.trim();
if (name.includes(word)) {
found = {
index: i,
content: tr.textContent,
};
}
return found;
});
return found;
}
const found = findMatchingRow("Australia");
if (found) {
console.log("found at:"+ JSON.stringify(found));
console.log("found at:"+ found.index);
}
you only search in text-Content of the whole tr (row). text-Content are all nodes. Australia results in an Array, cause many entries have "Australia/Oceania" as data-continent attr.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.