我想抓取通過另一個域的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的孩子網址是
有沒有什麼辦法讓我可以訪問的iframe content.I希望這個數據表格分析而不是任何非法使用。請幫助我
如果使用JavaScript動態加載註釋,則cURL或PHP將無法神奇加載它們。您需要使用[PhantomJS](http://phantomjs.org/)等模擬瀏覽器加載頁面,然後從中提取結果。 –
這不完全是這種情況。你可以得到前20條評論,之後你不能只使用Curl – PHPhil
@PHPhil謝謝你的回覆,但你能幫我通過修改我的代碼來獲得前20條評論,這將是一個很好的臨時解決方案。 – user3818862