【发布时间】:2015-10-11 06:40:32
【问题描述】:
我正在尝试从另一个域中抓取通过 iframe 生成的 cmets。 当我尝试这样做时,我要么收到一条空消息,说明此应用程序未注册。我知道这是由于跨域问题造成的。我使用 Curl 在 php 中编写了以下代码。当我传递父级时url 它加载页面,但 iframe 下的内容丢失,当我传递子 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 是
我有什么方法可以访问 iframe 内容。我想要这种数据形式分析,而不是用于任何非法用途。
提前感谢您的帮助。
【问题讨论】:
-
如果 cmets 是使用 JavaScript 动态加载的,那么 cURL 或 PHP 将无法神奇地加载它们。您需要使用 PhantomJS 之类的东西来模拟加载页面的浏览器,然后从中提取结果。
-
这里并非完全如此。您可以获得前 20 个 cmets,之后您就不能只使用 Curl
-
@PHPhil 感谢您的回复,但您能否通过修改我的代码来帮助我获得前 20 个 cmets,这将是一个很好的临时解决方案。
-
@Mr.Llama 如果我按照建议使用 PhantomJS,我是否能够浏览子 iframe,否则我可能会因跨域问题而被拒绝
标签: javascript php html curl iframe