2015-07-21 84 views
0

我想抓取通過另一個域的iframe生成的註釋。 當我試圖這樣做時,我要麼得到一個空消息,說這個應用程序沒有註冊。我明白,這是由於跨域問題。我寫了下面的代碼在PHP中使用Curl.When我通過父url它加載頁面,但iframes下的內容丟失,當我傳遞子網址時,它返回一條消息說應用程序未註冊。使用curl刮取iframe內容php

代碼:

<?php 

// 1. initialize 

$ch = curl_init(); 

// 2. The URL containing the iframe 

$url = "http://www.ndtv.com/india-news/1993-mumbai-blasts-convict-yakub- memons-final-mercy-plea-rejected-783656?pfrom=home-lateststories"; 

// 3. set the options, including the url 

curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 2); 
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 

// 4. execute and fetch the resulting HTML output by putting into $output 
$output = curl_exec($ch); 

// 5. free up the curl handle 
curl_close($ch); 

// 6. Scrape for a single string/word ("Paris") 
preg_match("~</?p[^>]*>~", $output, $match); 
    if($match) 

// 7. Display the scraped string 
echo $output; 
?> 

iframe的孩子網址是

http://social.ndtv.com/static/Comment/Widget/?&key=68a2a311a51a713dad2e777d65ec4db4&link=http%3A%2F%2Fwww.ndtv.com%2Findia-news%2F1993-mumbai-blasts-convict-yakub-memons-final-mercy-plea-rejected-783656&title=Yakub+Memon+to+Hang+On+July+30+for+India%27s+Deadliest+Terror+Attack&ctype=story-news&identifier=story-news-783656&enableCommentsSubscription=1&ver=1&reply=1&sorted_by=likes

有沒有什麼辦法讓我可以訪問的iframe content.I希望這個數據表格分析而不是任何非法使用。請幫助我

+0

如果使用JavaScript動態加載註釋,則cURL或PHP將無法神奇加載它們。您需要使用[PhantomJS](http://phantomjs.org/)等模擬瀏覽器加載頁面,然後從中提取結果。 –

+0

這不完全是這種情況。你可以得到前20條評論,之後你不能只使用Curl – PHPhil

+0

@PHPhil謝謝你的回覆,但你能幫我通過修改我的代碼來獲得前20條評論,這將是一個很好的臨時解決方案。 – user3818862

回答

0

您需要實際解析HTML ...正則表達式不適用於html。

參見:RegEx match open tags except XHTML self-contained tags

+0

這不是問題,因爲我無法瀏覽我的iframe,因爲存在交叉瀏覽問題有什麼建議麼??? – user3818862

+0

啊。誤解。如果你捲曲的iframe網址? –

+0

對不起,當我捲曲的iframe網址它說應用程序未註冊,這是因爲iframe位於另一個域 – user3818862

0

如果你想討論的意見,然後需要獲取註釋部分的iframe網址,而不是包含的iframe頁面。 cURL只是返回URL的源代碼,它不遞歸地跟隨iframe鏈接並嵌入它們。

+0

我嘗試傳遞iframe url,但它返回一條消息,說應用程序未註冊。請幫助 – user3818862