我想抓取通过另一个域的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