Search code examples
phphtmlcurlxpathnormalize-space

XPATH - normalize-space with more than 1 text for counting purposes


Hi everyone i'm noob at this,

https://decisoesesolucoes.com/agencias/albergaria/consultores

In the url above, i want to count the number of 'consultor imobiliario' and 'Consultora Imobiliaria' , both the text has spaces, so why im using the normalize-space .

The text i want to get

Example:

"//*[text()[normalize-space() = 'consultor imobiliario']]" - this works

But if i want to count also the 'Consultora Imobiliaria' doesn't work:

"//*[text()[normalize-space() = 'consultor imobiliario' and 'Consultora Imobiliária']]"  

(if I user OR instead AND the counting = bad count)

My intire code is :

$current_page = 1;
$max_page = 999999999999;
$countTotalConsultores=0;

while($max_page >= $current_page){

$url = "https://decisoesesolucoes.com/agencias/albergaria/consultores?page=";
$url .= $current_page;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$res = curl_exec($ch);

curl_close($ch);

$dom = new DomDocument();
@ $dom->loadHTML($res);

$xpath = new DOMXpath($dom);
$body = $xpath->query("//*[text()[normalize-space() = 'consultor imobiliario' and 'Consultora Imobiliária']]"); 
$count = $body->length;

$countTotalConsultores = $countTotalConsultores+$count;

echo "        Página atual:" .$current_page . "No. of agents " . $countTotalConsultores;

$current_page = $current_page+1;

if ($count < 1){
    break;

Anyone can help me please?


Solution

  • i think you're looking for

    "//*[text()[contains(normalize-space(), 'consultor imobiliario') or contains(normalize-space(),'Consultora Imobil')]]"