Parse element from redirected page with PHP Simple HTML DOM Parser

Viewed 256

I'm trying to parse a title from redirected page. Here is my code:

<?php

include_once('simple_html_dom.php');

$link="https://duckduckgo.com/?q=!ducky+google";
$html = file_get_html($link);

foreach ($html->find('title') as $text){
    echo $text->plaintext."<br/>";

}
?>

The result should be "Google". Thanks

2 Answers

I'm not sure I 100% understood your request, but here are a few things to help you move on !

Three things :

  1. the "!" in the $link redirects you to google. Delete it if you want to access the ducky result page.
  2. simple-html-dom can't access the ducky result page. Did you try to echo the $html to see what you get ? I tried and was blocked by a captcha ... you'll need to figure out how to bypass it. Then and only then you'll have access to the titles.
  3. Finally, your titles are H2 ... it might be easier to reach h2 tags with the parser.

Does this help ? If you find a way to bypass the captcha let me know ! I'm interested :)

After use Simle DOM find URL redirected page :

<?php 

$url="https://duckduckgo.com/?q=!ducky+google";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Must be set to true so that PHP follows any "Location:" header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$a = curl_exec($ch); // $a will contain all headers

$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // This is what you need, it will return you the last effective URL



echo $url; // your url hire *_*  AppsGM
?>

AND now use your code

    <?php

include_once('simple_html_dom.php');


$html = file_get_html($url);

foreach ($html->find('title') as $text){
    echo $text->plaintext."<br/>";

}
?>
Related