2010-11-13 145 views
3

我正在使用PHP试图抓取页面,似乎在父页面加载完成后仅仅几毫秒就动态加载内容。PHP:延迟解析页面源代码(通过file_get_html())1秒

我使用curl来解析页面,而simpleHtmlDom从解析的html中抓取事物。我的努力遍历DOM和爆炸()的东西出来的HTML返回什么都没有。我唯一的想法是加载了之后的加载了父页面。

这是我的代码。

<? 
$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之前?或者,也许有人有更好的见解?

感谢

马克

+2

任何类型的延迟都与您当前拥有的错误消息无关。 – zerkms 2010-11-13 03:39:39

+1

顺便说一句,刮脸在Facebook的任何应用程序的内容是超出他们的TOS和非法,downvoted。 – zerkms 2010-11-13 03:40:15

+0

Upvoted回零。这不是一个法律咨询网站。除了Facebook没有对用户贡献的内容拥有版权的权力之外,对个人使用的欺骗也很少是非法的。 (是的,我知道我也在做假设。) – mario 2010-11-13 04:11:43

回答

1

做一个简单的延迟

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

你真的应该重新读取错误消息。它不是源于时间问题。

您从curl中获得一个$ html字符串。但是你不能调用phphtmldom函数 - >马上找到它。你必须在遍历之前解析它。另外还不清楚你为什么首先使用卷曲。请仅使用$dom = str_get_html($html)或尝试:

$dom = file_get_html('http://www.facebook.com/OneAndroidAppaDay'); 

$bituurl = $dom->find('div[class=UIStoryAttachment_Title]',0)->... 
+0

我过去一直在使用file_get_html(),它仍然会抛出相同的错误。我认为它只是试图去探索那些还没有的东西。 – 2010-11-13 19:11:20

+0

@ marky-b:那肯定是一个simplehtmldom的bug。 'print_r'无论你回来看看它是否是一个对象。否则,请尝试解析str_variant。或者更好的迁移到phpQuery或QueryPath,这是两种不错的选择。 – mario 2010-11-13 19:27:30