Reputation: 16906
I have a moderately simple problem, and I am trying to figure out the best approach to implement it.
For those wondering, this is used to scrape data from a non-editable page (can add css,js, but the rest of the content is closed source and not available for editing)
I am iterating through an element (tr
for example), and I only want to look at the cases where a certain number of td
elements are present.
Eg, For every tr
, I only want to consider the case where it has 3 td
children
What is the best way to do that?
Upvotes: 2
Views: 3685
Reputation: 23943
You could try something like this:
var result = $('tr').filter( function(){
return $(this).children('td').length === 3;
});
You can then do any kind of scraping you like. A simple example:
result.each( function(i,e){ // for each row
// e = one of the selected rows
$(e).children('td').each( function(i,e){ // for each td in the row
your_scraping_function( e.text() ); // etc.
});
});
As @patrick points out below, you can of course chain the scraping process directly to the output of the filter()
if it makes sense for your code.
Edit: Added 'td'
selector in children()
. Belt and suspenders.
Edit: Added example of how to walk through results, as per OP comment.
Upvotes: 5