简体   繁体   中英

How do I wait for my script to load the page with PHP Simple HTML DOM Parser?

I'm making a call to a website where they have a javascript loading code. When I launch the script I only collect the data that is already loaded but it doesn't wait for it to load the whole page.

I try to collect the data shown in some div to make a statistic in my backend from the web of trivago, which has a loader in the. To do this I launch the following code:

include_once('simple_html_dom.php');

function getHTML($url,$timeout){
  $ch = curl_init($url); // initialize curl with given url
  curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTPS_USER_AGENT"]); // set  
  useragent
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute
  curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error
  return @curl_exec($ch);
}

$html = file_get_html("https://www.trivago.es/?aDateRange%5Barr%5D=13-04-2019&aDateRange%5Bdep%5D=14-04-2019&iPathId=82650&iGeoDistanceItem=0&aCategoryRange=0%2C1%2C2%2C3%2C4%2C5&aOverallLiking=1%2C2%2C3%2C4%2C5&sOrderBy=relevance%20desc&iRoomType=7&cpt=8265003&iViewType=0&bIsSeoPage=false&bIsSitemap=false&");

I try to collect the data with the find() function:

foreach($html->find("div.item__flex-column") as $seccion) {
  echo "<tr>";
    echo "<td>";
      echo $seccion->find("h3",0)->plaintext;
    echo "</td>";
    echo "<td>";
      echo $seccion->find("p.details__paragraph",0)->plaintext;
    echo "</td>";
    echo "<td>";
      echo $seccion->find("strong.item__best-price",0)->plaintext; 
    echo "</td>";
    echo "<td style='text-decoration:line-through;'>";
      echo $seccion->find($fmp,0)->plaintext; 
    echo "</td>";
  echo "</tr>";
}

And the mistake I get:

Fatal error: Uncaught Error: Call to a member function find() on string

Is there any way to stop the PHP program until it loads the whole page?

尝试

echo $seccion->find("h3")[0]->plaintext;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM