Web scrapping páginas amarillas

 <?php

        function extraeRegexp($cadena, $inicio, $fin) {
            preg_match_all('#' . $inicio . '(.*?)' . $fin . '#', $cadena, $matches);
            return $matches[1];
        }

        $total = [];
        for ($i = 1; $i <= 6; $i++) {
            $url = "https://www.paginasamarillas.es/search/mascotas-y-tiendas-de-animales/all-ma/barcelona/all-is/barcelona/all-ba/all-pu/all-nc/$i?what=mascotas+y+tiendas+de+animales&where=barcelona&ub=false&qc=true";
            $web = file_get_contents($url);
            $urls = extraeRegexp($web, "<div class=\"envio-consulta\"><a href=\"", "\"");
            $total = array_merge($total, $urls);
        }
        $fichero = fopen("mascotas.csv", "w");
        fwrite($fichero, utf8_decode( "Nombre;Categoria;Telefono;Direccion;Cp;Ciudad;Provincia;Web\n"));
        foreach ($total as $url) {
            $web = file_get_contents($url);
            $nombre = extraeRegexp($web, '<h1 itemprop="name">', '</h1>')[0];
            $categoria = extraeRegexp($web, '<span class="category">', '</span>')[0];
            $telefono = extraeRegexp($web, '<span itemprop="telephone">', '</span>')[0];
            $direccion = extraeRegexp($web, '<span itemprop="streetAddress">', '</span>')[0];
            $cp = extraeRegexp($web, '<span itemprop="postalCode">', '</span>')[0];
            $ciudad = extraeRegexp($web, '<span itemprop="addressLocality">', '</span>')[0];
            $provincia = @extraeRegexp($web, '<span class="addressState">', '</span>')[0];
            $webaddress = @str_replace("?utm_campaign=paginasamarillas&utm_source=paginasamarillas&utm_medium=referral", "", extraeRegexp($web, 'class="fa icon-link"></i><a href="', '"')[0]);

            echo $nombre . ";" . $categoria . ";" . $telefono . ";" . $direccion . ";" . $cp . ";" . $ciudad . ";" . $provincia . ";" . $webaddress . "<br/>";
            fwrite($fichero, utf8_decode($nombre . ";" . $categoria . ";" . $telefono . ";" . $direccion . ";" . $cp . ";" . $ciudad . ";" . $provincia . ";" . $webaddress . "\n"));

        }
        fclose($fichero);
        ?>