gpt4 book ai didi

PHP:延迟解析页面源代码(通过 file_get_html())1 秒

转载 作者:搜寻专家 更新时间:2023-10-31 21:45:39 25 4
gpt4 key购买 nike

我正在使用 PHP 尝试抓取一个页面,该页面似乎在父页面完成加载后的几毫秒内动态加载内容。

我正在使用 curl 来解析页面,并使用 simpleHtmlDom 从解析的 html 中抓取内容。

我在 html 中遍历 DOM 和 explode() 的努力没有返回任何结果。我唯一的想法是它在加载父页面后 加载内容。

这是我的代码。

<? 
$url = 'http://www.facebook.com/OneAndroidAppaDay';
$scrapeUrl = 'http://www.facebook.com/OneAndroidAppaDay';

include_once('simple_html_dom.php');
require_once("bitly.php");

$userAgent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$scrapeUrl);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html = curl_exec($ch);
if (!$html) {
echo "<br />cURL error number:" .curl_errno($ch);
echo "<br />cURL error:" . curl_error($ch);
exit;
}

$appBitlyUrl = $html->find('div[class=UIStoryAttachment_Title]',0)->find('a',0)->href; // fail :(
echo 'Bitly Url: ' . $appBitlyUrl;
?>

它在第 24 行(用内联注释表示)被炸毁并出现以下错误:

Fatal error: Call to a member function find() on a non-object in /home/xxxxxxxx/public_html/xxx.xx/xxxx.php on line 24

有没有办法让它在抓取页面的 html 之前等待一两秒钟?或者也许有人有更好的见解?

谢谢

标记

最佳答案

做一个简单的延迟

sleep(2); // 2 second delay before continuing

关于PHP:延迟解析页面源代码(通过 file_get_html())1 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4170820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com