0
I’m picking up through a parser with PHP DOM all the links on a page. I need to pick up only the links that contain the word "buy-Toyota"
I was using the following code, but suddenly it stopped working:
//array que pega o valor do parser
$toyota =array();
$dom1 = new DOMDocument();
$dom1->loadHTMLFile('http://www.webmotors.com.br/comprar/carros/novos-usados/sp-sao-paulo/toyota/?tipoveiculo=carros&anunciante=pessoa%20f%C3%ADsica&tipoanuncio=novos%7Cusados&estado1=s%C3%A3o%20paulo&marca1=toyota&anode=2012&anoate=&kmde=&kmate=30000&p=1&o=1&qt=36');
// Consultando os links
$links1 = $dom1->getElementsByTagName('a');
foreach ($links1 as $link) {
//aqui pega o link
$string = $link->getAttribute('href').PHP_EOL;
//apenas verifica se o link nao tem a string "comprar/toyota"
if(strpos($string, 'comprar/toyota') != 1){
//nadafaz;
}else{
//verifica se o link tem a string "comprar/toyota"
if(strpos($string, 'comprar/toyota') == 1){
//guarda a string encontrada
$linky = $string;
$n++;
#echo $n."<br>";
//insere o link no array
$toyota[$n] = $linky;
//imprime oo link
echo $toyota[$n]."<br>";
}
}
}
Then I reversed the strpos condition on my staying so:
//array que pega o valor do parser
$toyota =array();
$dom1 = new DOMDocument();
$dom1->loadHTMLFile('http://www.webmotors.com.br/comprar/carros/novos-usados/sp-sao-paulo/toyota/?tipoveiculo=carros&anunciante=pessoa%20f%C3%ADsica&tipoanuncio=novos%7Cusados&estado1=s%C3%A3o%20paulo&marca1=toyota&anode=2012&anoate=&kmde=&kmate=30000&p=1&o=1&qt=36');
// Consultando os links
$links1 = $dom1->getElementsByTagName('a');
foreach ($links1 as $link) {
//aqui pega o link
$string = $link->getAttribute('href').PHP_EOL;
//apenas verifica se o link nao tem a string "comprar/toyota"
if(strpos($string, 'comprar/toyota') == 1){
//nadafaz;
}else{
//verifica se o link tem a string "comprar/toyota"
if(strpos($string, 'comprar/toyota') != 1){
//guarda a string encontrada
$linky = $string;
$n++;
#echo $n."<br>";
//insere o link no array
$toyota[$n] = $linky;
//imprime oo link
echo $toyota[$n]."<br>";
}
}
}
Now it’s pulling all the links from the page...I needed to get only the ones that have the string "buy-Toyota" anywhere in the link
How can I fix this?
Thank you so much beast! It was accurate...
– Charles Fay
Contemplates the desired by AP, but is not 100% correct.
– Guilherme Lautert
@Guilhermelautert, Voce was right. The worst I had used type comparison in the test I did, but when I posted the answer I used > 0 .
– Marcos Regis