Created
November 17, 2015 00:06
-
-
Save JaviPedrera/ce517d59c1d4210fc872 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Html | |
$html = file_get_contents('https://es.search.yahoo.com/search?p=madrid&fr=yfp-t-777'); | |
// Creating instance of DOMDocument and loading the HTML | |
$dom = new DOMDocument; | |
libxml_use_internal_errors(TRUE); | |
$dom->loadHTML($html); | |
libxml_use_internal_errors(FALSE); | |
// Getting the container with the desired results | |
$container = $dom->getElementById('web'); | |
// Fetching results grouped by tags | |
$titles = $container->getElementsByTagName('h3'); | |
$links = $container->getElementsByTagName('a'); | |
$cache = $container->getElementsByTagName('span'); | |
$bodies = $container->getElementsByTagName('p'); | |
// Getting the number of results | |
$resultsCount = 0; | |
foreach ($container->getElementsByTagName('h3') as $title) { | |
$resultsCount++; | |
} | |
// Iterating and printing tag values | |
$i = 0; | |
while ($i < $resultsCount) { | |
echo $titles[$i]->nodeValue; | |
echo "<br/>"; | |
echo $links[$i]->nodeValue . " - " . $cache[$i]->nodeValue; | |
echo "<br/>"; | |
echo $bodies[$i]->nodeValue; | |
echo "<br/>"; | |
echo "<br/>"; | |
$i++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment