2015-07-21 131 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