I'm currently trying to scrape all the results from a table within a webpage. At the moment I'm attempting it with file_get_contents()
and some jquery but I'm unable to seem to find the selectors within the DOM
with jQuery.
I'm trying to get them row by row as I intend on inserting them into my database for future use.
My question is how would I get the values of each <td>
within each <tr>
within the table on the webpage in order to insert all of these values into my database?
PHP:
Controller
function scqf_stats(){
$data['stats'] = file_get_contents("http://www.scqf.org.uk/Search%20The%20Database?ssub=&stit=Enter+a+title+or+a+part+of+it&sown=Start+typing+and+select+from+dropdown&sownid=&slev=&scrb=&sk=&submitsp=Search");
$data['main_content'] = 'alt_test';
$this->load->view('templates/single_view', $data);
}
View
<pre>
<script src="<?php echo base_url() ?>js/scrape.js"></script>
<?php print_r($stats); ?>
</pre>
jQuery:
$(document).ready(function() {
function scrape_it(){
$('#search-database-results').children('tbody').children('tr').each(function (){
$this = $(this);
$('tr').children('td').each(function() {
var text = $('td').text();
console.log(text);
});
});
}
scrape_it();
});
Assuming you have the scraped page in an element of id=search-database-results
this should do to find all TDs within each TR within the table (in order):
$(document).ready(function() {
function scrape_it(){
$('#search-database-results tr td').each(function() {
var text = $(this).text();
console.log(text);
});
}
scrape_it();
});
If you need more/different, please explain in more detail.