CA
rival-black

Scraping for the text in a list item

Currently I try to scrape for the text in a list item (see picture). But with my implementet logic I can only scrape for the <b> tag with the text "Number of Outputs". How can I reach to the following text " : 5"? Here is my loop for scraping the text of every "li.li-item" :
$('li.li-item').each((index, element) => {
const jElement = $(element);
const ValueName = jElement.find('b').eq(0)?.text()?.trim();
const Value = jElement.find('#text').eq(0)?.text()?.trim();

if(ValueName){
scrapedProducts[ValueName]= Value || '';
}
});
$('li.li-item').each((index, element) => {
const jElement = $(element);
const ValueName = jElement.find('b').eq(0)?.text()?.trim();
const Value = jElement.find('#text').eq(0)?.text()?.trim();

if(ValueName){
scrapedProducts[ValueName]= Value || '';
}
});
No description
5 Replies
conscious-sapphire
conscious-sapphire3y ago
There are 2 ways to do this: 1. Scrape the text of the whole .li-item element and then remove the Number of Outputs text from it
const wholeText = $('.li-item').text();
const number = Number(wholeText.replace('Number of Outputs', '').replace(':', '').trim())
const wholeText = $('.li-item').text();
const number = Number(wholeText.replace('Number of Outputs', '').replace(':', '').trim())
2. Get the non-element nodes with .contents()
// not sure what is the correct index, just test that
const number = Number($('.li-item').contents()[3])
// not sure what is the correct index, just test that
const number = Number($('.li-item').contents()[3])
rival-black
rival-blackOP3y ago
Now I can scrape for the whole text but the Value is always only the text from the last li.item.. How can I add the right value to the right specsname?
$('li.li-item').each((index, element) => {
const jElement = $(element);
const ValueName = jElement.find('b').eq(0)?.text()?.trim();
const wholeText = $('.li-item').text();
const Value = wholeText.replace(/.*: /g, '').trim()

// const Value = wholeText.replace(/.*: /g, '').trim()

if(ValueName){
scrapedProducts[ValueName]= Value || '';
}
});
$('li.li-item').each((index, element) => {
const jElement = $(element);
const ValueName = jElement.find('b').eq(0)?.text()?.trim();
const wholeText = $('.li-item').text();
const Value = wholeText.replace(/.*: /g, '').trim()

// const Value = wholeText.replace(/.*: /g, '').trim()

if(ValueName){
scrapedProducts[ValueName]= Value || '';
}
});
No description
No description
rival-black
rival-blackOP3y ago
MAXPRO® NVR Software | System Agreements & Upgrades | Security Cont...
Learn all about the Honeywell Security MAXPRO® NVR Software. Click to find product details, documentation, ordering info and more.
conscious-sapphire
conscious-sapphire3y ago
If you are doing anything in a loop like $('li.li-item') all selectors inside the loop must reference the looped element so in your code it must start with jElement. In this case wholeText = jElement.text();
rival-black
rival-blackOP3y ago
Thank you just did not see that. Now it works perfectly!

Did you find this page helpful?