How to Scrap a Table and insert the data into the database?

Asked

Viewed 32 times

-1

Well, basically what I need is to do a microservice that extracts the prices from the soybean quotation from this table: https://www.canalrural.com.br/cotacao/soja/. And then insert the data into a database. Although I can scraping using the code below, I have no idea how to "filter" the data of the array to include in the database structure.

$content = file_get_contents('https://www.canalrural.com.br/cotacao/soja/');

preg_match_all('/<table class="table table-striped table-cotacao first-col-small table-menor">(.*?)(.*?)<\/div>/s',$content,$matches);

foreach ($matches[0] as $key => $value){
    echo $value;
    echo '<hr>';
}

Database columns are only the location and price.

1 answer

-2

Oops, I advise you to use the library Simple HTML DOM Parser, then you can interact with html much easier, using css selectors, like javascript.

In your case, you could use it this way:

$html = file_get_html('https://www.canalrural.com.br/cotacao/soja/');
$tableRows = $html->find('.table-cotacao tr'); // pega todas as <tr> dentro da .table-cotacao

foreach($tableRows as $tr) {
  $name  = $tr->find('td')[0]->plaintext;
  $price = $tr->find('td')[1]->plaintext;
}

I hope I helped =D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.