使用Curl抓取iframe内容



我正试图从另一个域通过iframe生成的评论。当我试图这样做时,我要么得到一个空消息,说这个应用程序没有注册。我理解这是由于跨域问题。我用Curl在php中编写了以下代码。当我传递父url时,它加载页面,但iframes下的内容缺失,当我传递子url时,它返回一条消息说应用程序未注册。

代码:

<?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的子url是

http://social.ndtv.com/static/Comment/Widget/?&关键= 68 a2a311a51a713dad2e777d65ec4db4&链接3 = http % % 2 f % 2 fwww.ndtv.com % 2 findia-news % 2 f1993 -孟买爆炸案罪犯- Yakub memons -最后的怜悯——请求拒绝- 783656,标题= Yakub + Memon + + +挂在+ + 30 + + 7月印度+致命+恐怖+ Attack& ctype = story-news&标识符=故事-新闻- 783656 -,enableCommentsSubscription = 1,版本= 1,回复= 1,sorted_by =喜欢

是否有任何方法可以访问iframe内容?我想要这些数据进行分析,而不是用于任何非法用途。

提前感谢您的帮助

您需要实际解析HTML…正则表达式不适合html。

参见:RegEx匹配除XHTML自包含标签以外的打开标签

如果你想要讨论评论,那么需要获取评论部分的iframe URL,而不是包含iframe的页面。cURL只是返回URL的源代码,它不会递归地跟踪iframe链接并嵌入它们。

最新更新