Taking data from a PHP DOM site

Asked

Viewed 133 times

1

Hello, I’m trying to get data from a website to an internal system I’m creating through PHP I’m using the DOM for this.

I was able to find all the information in his html, but I’m not able to filter.

My code:

    <?php
// Create DOM from URL or file
include 'simple_html_dom.php';

echo file_get_html('http://www.site.com.br/')->plaintext;

$html = file_get_html('http://www.site.com.br/');

// Aqui está meu erro
foreach($html->find('main') as $element) 
       echo $element->select . '<br>';

// Find all links 
//foreach($html->find('a') as $element) 
      // echo $element->href . '<br>';
  ?>

My website returns:

Concordia Herval Erechim Kaiser lata 350ml R$ 1,69 /cada Costela grossa resfriada R$ 11,99 /kg Açúcar cristal Guarani 5kg R$ 6,95 /cada Leite longa vida Terra Viva 1L R$ 2,09 /cada Filé americano R$ 14,99 /kg Coxa com sobrecoxa dorsal R$ 3,99 /kg Cerveja Bohemia lata 350ml R$ 1,99 /cada Filé simples R$ 17,99 /kg Alcatra com osso R$ 21,90 /kg Kaiser lata 350ml R$ 1,69 /cada Costela grossa resfriada R$ 11,99 /kg Sabão em pó Omo multi ação 2kg R$ 13,99 /cada Leite longa vida Tirol 1L R$ 2,19 /cada Alcatra com osso R$ 21,90 /kg Carré suíno com pele resfriado R$ 10,90 /kg Filé simples R$ 17,90 /kg Budweiser lata 350ml R$ 2,79 /cada Leite condensado Tirol 395g R$ 2,79 /cada Cerveja Dado Bier latão 473ml R$ 2,79 /cada Alcatra sem osso R$ 24,90 /kg Coca-Cola 2L R$ 5,49 /cada Leite condensado Tirol 395g R$ 2,89 /cada Costela suína com pele resfriada R$ 9,90 /kg Cerveja Lokal latão 473ml R$ 1,89 /cada Açúcar cristal Gasparin 5kg R$ 7,99 /cada Fraldinha bovina Montana R$ 19,90 /kg Filé americano R$ 15,90 /kg Acesse nosso tablóide de ofertas

But I would like to take only from my city which is Erechim, which would start in Given Beer.

The structure of the site is like this:

<main id="main" class="site-main" role="main">
            <select id="cidade-ofertas">
                <option value="cidade-concordia">Concordia</option>
                <option value="cidade-herval">Herval</option>
                <option value="cidade-erechim">Erechim</option>
            </select>
    <div class="products-slider cidade-erechim">
                                                            <div class="item">
                                    <header class="item-header">
                                        <div class="item-thumb">
                                            <img width="160" height="160" src="http://www.superpassarela.com.br/wp-content/uploads/2017/12/cerveja-dadobier-lata-473ml-copy-160x160.png" class="attachment-home_product size-home_product wp-post-image" alt="" srcset="http://www.superpassarela.com.br/wp-content/uploads/2017/12/cerveja-dadobier-lata-473ml-copy-160x160.png 160w, http://www.superpassarela.com.br/wp-content/uploads/2017/12/cerveja-dadobier-lata-473ml-copy-150x150.png 150w, http://www.superpassarela.com.br/wp-content/uploads/2017/12/cerveja-dadobier-lata-473ml-copy-300x300.png 300w, http://www.superpassarela.com.br/wp-content/uploads/2017/12/cerveja-dadobier-lata-473ml-copy-180x180.png 180w, http://www.superpassarela.com.br/wp-content/uploads/2017/12/cerveja-dadobier-lata-473ml-copy-40x40.png 40w, http://www.superpassarela.com.br/wp-content/uploads/2017/12/cerveja-dadobier-lata-473ml-copy-60x60.png 60w, http://www.superpassarela.com.br/wp-content/uploads/2017/12/cerveja-dadobier-lata-473ml-copy.png 400w" sizes="(max-width: 160px) 100vw, 160px" />
                                            <h4 class="item-title">Cerveja Dado Bier latão 473ml</h4>
                                        </div><!-- .item-thumb -->

                                        <p class="item-description">
                                            <span class="price"><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">&#82;&#36;</span>2,79</span></span> <span class="detail">/cada</span>
                                        </p><!-- .item-description -->
                                    </header><!-- .item-header -->
                                </div><!-- .item -->
                                                            <div class="item">
                                    <header class="item-header">
                                        <div class="item-thumb">
                                            <img width="160" height="160" src="http://www.superpassarela.com.br/wp-content/uploads/2018/09/alcatra-sem-osso-160x160.png" class="attachment-home_product size-home_product wp-post-image" alt="" srcset="http://www.superpassarela.com.br/wp-content/uploads/2018/09/alcatra-sem-osso-160x160.png 160w, http://www.superpassarela.com.br/wp-content/uploads/2018/09/alcatra-sem-osso-150x150.png 150w, http://www.superpassarela.com.br/wp-content/uploads/2018/09/alcatra-sem-osso-300x300.png 300w, http://www.superpassarela.com.br/wp-content/uploads/2018/09/alcatra-sem-osso-40x40.png 40w, http://www.superpassarela.com.br/wp-content/uploads/2018/09/alcatra-sem-osso-60x60.png 60w, http://www.superpassarela.com.br/wp-content/uploads/2018/09/alcatra-sem-osso.png 400w, http://www.superpassarela.com.br/wp-content/uploads/2018/09/alcatra-sem-osso-100x100.png 100w" sizes="(max-width: 160px) 100vw, 160px" />
                                            <h4 class="item-title">Alcatra sem osso</h4>
                                        </div><!-- .item-thumb -->

                                        <p class="item-description">
                                            <span class="price"><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">&#82;&#36;</span>24,90</span></span> <span class="detail">/kg</span>
                                        </p><!-- .item-description -->
                                    </header><!-- .item-header -->
                                </div><!-- .item -->
                                                            <div class="item">
                                    <header class="item-header">
                                        <div class="item-thumb">
                                            <img width="160" height="160" src="http://www.superpassarela.com.br/wp-content/uploads/2017/12/Coca-Cola-2-litros-4-160x160.png" class="attachment-home_product size-home_product wp-post-image" alt="" srcset="http://www.superpassarela.com.br/wp-content/uploads/2017/12/Coca-Cola-2-litros-4-160x160.png 160w, http://www.superpassarela.com.br/wp-content/uploads/2017/12/Coca-Cola-2-litros-4-150x150.png 150w, http://www.superpassarela.com.br/wp-content/uploads/2017/12/Coca-Cola-2-litros-4-300x300.png 300w, http://www.superpassarela.com.br/wp-content/uploads/2017/12/Coca-Cola-2-litros-4-180x180.png 180w, http://www.superpassarela.com.br/wp-content/uploads/2017/12/Coca-Cola-2-litros-4-40x40.png 40w, http://www.superpassarela.com.br/wp-content/uploads/2017/12/Coca-Cola-2-litros-4-60x60.png 60w, http://www.superpassarela.com.br/wp-content/uploads/2017/12/Coca-Cola-2-litros-4.png 400w" sizes="(max-width: 160px) 100vw, 160px" />
                                            <h4 class="item-title">Coca-Cola 2L</h4>
                                        </div><!-- .item-thumb -->

                                        <p class="item-description">
                                            <span class="price"><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">&#82;&#36;</span>5,49</span></span> <span class="detail">/cada</span>
                                        </p><!-- .item-description -->
                                    </header><!-- .item-header -->
                                </div><!-- .item -->
                                                            <div class="item">
                                    <header class="item-header">
                                        <div class="item-thumb">
                                            <img width="160" height="160" src="http://www.superpassarela.com.br/wp-content/uploads/2018/01/leite-condensado-tirol-tp395-160x160.png" class="attachment-home_product size-home_product wp-post-image" alt="" srcset="http://www.superpassarela.com.br/wp-content/uploads/2018/01/leite-condensado-tirol-tp395-160x160.png 160w, http://www.superpassarela.com.br/wp-content/uploads/2018/01/leite-condensado-tirol-tp395-150x150.png 150w, http://www.superpassarela.com.br/wp-content/uploads/2018/01/leite-condensado-tirol-tp395-300x300.png 300w, http://www.superpassarela.com.br/wp-content/uploads/2018/01/leite-condensado-tirol-tp395-180x180.png 180w, http://www.superpassarela.com.br/wp-content/uploads/2018/01/leite-condensado-tirol-tp395-40x40.png 40w, http://www.superpassarela.com.br/wp-content/uploads/2018/01/leite-condensado-tirol-tp395-60x60.png 60w, http://www.superpassarela.com.br/wp-content/uploads/2018/01/leite-condensado-tirol-tp395.png 400w" sizes="(max-width: 160px) 100vw, 160px" />
                                            <h4 class="item-title">Leite condensado Tirol 395g</h4>
                                        </div><!-- .item-thumb -->

                                        <p class="item-description">
                                            <span class="price"><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">&#82;&#36;</span>2,89</span></span> <span class="detail">/cada</span>
                                        </p><!-- .item-description -->
                                    </header><!-- .item-header -->
                                </div><!-- .item -->
                                                            <div class="item">
                                    <header class="item-header">
                                        <div class="item-thumb">
                                            <img width="160" height="160" src="http://www.superpassarela.com.br/wp-content/uploads/2018/09/costela_suina-160x160.png" class="attachment-home_product size-home_product wp-post-image" alt="" srcset="http://www.superpassarela.com.br/wp-content/uploads/2018/09/costela_suina-160x160.png 160w, http://www.superpassarela.com.br/wp-content/uploads/2018/09/costela_suina-150x150.png 150w, http://www.superpassarela.com.br/wp-content/uploads/2018/09/costela_suina-300x300.png 300w, http://www.superpassarela.com.br/wp-content/uploads/2018/09/costela_suina-40x40.png 40w, http://www.superpassarela.com.br/wp-content/uploads/2018/09/costela_suina-60x60.png 60w, http://www.superpassarela.com.br/wp-content/uploads/2018/09/costela_suina.png 400w, http://www.superpassarela.com.br/wp-content/uploads/2018/09/costela_suina-100x100.png 100w" sizes="(max-width: 160px) 100vw, 160px" />
                                            <h4 class="item-title">Costela suína com pele resfriada</h4>
                                        </div><!-- .item-thumb -->

                                        <p class="item-description">
                                            <span class="price"><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">&#82;&#36;</span>9,90</span></span> <span class="detail">/kg</span>
                                        </p><!-- .item-description -->
                                    </header><!-- .item-header -->
                                </div><!-- .item -->
                                                            <div class="item">
                                    <header class="item-header">
                                        <div class="item-thumb">
                                            <img width="160" height="160" src="http://www.superpassarela.com.br/wp-content/uploads/2018/10/cerveja-lokal-1-160x160.png" class="attachment-home_product size-home_product wp-post-image" alt="" srcset="http://www.superpassarela.com.br/wp-content/uploads/2018/10/cerveja-lokal-1-160x160.png 160w, http://www.superpassarela.com.br/wp-content/uploads/2018/10/cerveja-lokal-1-150x150.png 150w, http://www.superpassarela.com.br/wp-content/uploads/2018/10/cerveja-lokal-1-300x300.png 300w, http://www.superpassarela.com.br/wp-content/uploads/2018/10/cerveja-lokal-1-40x40.png 40w, http://www.superpassarela.com.br/wp-content/uploads/2018/10/cerveja-lokal-1-60x60.png 60w, http://www.superpassarela.com.br/wp-content/uploads/2018/10/cerveja-lokal-1.png 400w, http://www.superpassarela.com.br/wp-content/uploads/2018/10/cerveja-lokal-1-100x100.png 100w" sizes="(max-width: 160px) 100vw, 160px" />
                                            <h4 class="item-title">Cerveja Lokal latão 473ml</h4>
                                        </div><!-- .item-thumb -->

                                        <p class="item-description">
                                            <span class="price"><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">&#82;&#36;</span>1,89</span></span> <span class="detail">/cada</span>
                                        </p><!-- .item-description -->
                                    </header><!-- .item-header -->
                                </div><!-- .item -->
                                                            <div class="item">
                                    <header class="item-header">
                                        <div class="item-thumb">
                                            <img width="160" height="160" src="http://www.superpassarela.com.br/wp-content/uploads/2018/08/açucar-gasparin-160x160.png" class="attachment-home_product size-home_product wp-post-image" alt="" srcset="http://www.superpassarela.com.br/wp-content/uploads/2018/08/açucar-gasparin-160x160.png 160w, http://www.superpassarela.com.br/wp-content/uploads/2018/08/açucar-gasparin-150x150.png 150w, http://www.superpassarela.com.br/wp-content/uploads/2018/08/açucar-gasparin-300x300.png 300w, http://www.superpassarela.com.br/wp-content/uploads/2018/08/açucar-gasparin-40x40.png 40w, http://www.superpassarela.com.br/wp-content/uploads/2018/08/açucar-gasparin-60x60.png 60w, http://www.superpassarela.com.br/wp-content/uploads/2018/08/açucar-gasparin.png 400w, http://www.superpassarela.com.br/wp-content/uploads/2018/08/açucar-gasparin-100x100.png 100w" sizes="(max-width: 160px) 100vw, 160px" />
                                            <h4 class="item-title">Açúcar cristal Gasparin 5kg</h4>
                                        </div><!-- .item-thumb -->

                                        <p class="item-description">
                                            <span class="price"><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">&#82;&#36;</span>7,99</span></span> <span class="detail">/cada</span>
                                        </p><!-- .item-description -->
                                    </header><!-- .item-header -->
                                </div><!-- .item -->

How do I display Product Name and Price? I’m not getting on track to filter

  • Give me the website link

  • if you can open a chat, here I think it would not be nice

  • really, just use the Cheerio if possible, much better than php for this kind of thing. Even if only to play on a spreadsheet or similar and then consume it in php. Even more direct in the DOM class is more Roots yet.

1 answer

0


I arrived in a result, I will leave here, can help more people.

<?php

// Create DOM from URL or file
include 'simple_html_dom.php';
include 'conexao.php';


echo $mercado = 'Mercado que busquei os preços';

$data = setlocale( LC_ALL, 'pt_BR', 'pt_BR.iso-8859-1', 'pt_BR.utf-8', 'portuguese' ); 
date_default_timezone_set( 'America/Sao_Paulo' );
echo strftime( '%d/%m/%Y', strtotime('today') ); 

$html = file_get_html('http://sitedomercado.com.br/');


// Find 
$nomes = array();
$i = 0;

$valores = array();
$j = 0;
foreach($html->find('div[class=products-slider cidade-erechim]') as $element) {
    $element . '<br>';

    foreach($element->find('h4[class=item-title]')  as $element2) {
        $element2 . '<br>';
        $nome = str_replace('<h4 class="item-title">', '', $element2);
        $nome1 = str_replace('</h4>', '', $nome);
        $nomes[$i] = $nome1;

        $i++;
    }

}

foreach($element->find('p[class=item-description]') as $element3) {
    $element3 . '<br>';


    $valor = trim(str_replace("&#82;&#36;","",str_replace("/cada","",str_replace("/kg","",str_replace(",",".",strip_tags($element3)))))). '<br>';
    $valor2 =  floatval(preg_replace("/[^-0-9\.]/","",$valor));
    $valor3 = str_replace(',', '.', $valor2);
    $valores[$j] = $valor3;
    $j++;




}

echo '<br>';

for ($r=0; $r < count($valores) ; $r++) { 
    # code...
    $nomes[$r];
    $valores[$r];

    setlocale(LC_TIME, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese');
    date_default_timezone_set('America/Sao_Paulo');
    $extenso = strftime('%A, %d de %B de %Y', strtotime('today'));
    echo $data1 = date('d/m/Y');

    $sql = "INSERT INTO web.consulta_preco (mercado, data_atual,nome_produto,preco,data_extenso) VALUES ('$mercado',(TO_DATE('$data1', 'dd/mm/yyyy')),'$nomes[$r]',to_number($valores[$r]),'$extenso')";
    $stmt = oci_parse($conexao, $sql);

    oci_execute($stmt);

    echo 'executado';
}






?>

Browser other questions tagged

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