【问题标题】:How to scrape iframe content using cURL如何使用 cURL 抓取 iframe 内容
【发布时间】:2012-01-14 13:41:17
【问题描述】:

目标:我想使用 cURL 在 iframe 中抓取单词“Paris”。

假设您有一个包含 iframe 的简单页面:

<html>
<head>
<title>Curl into this page</title>
</head>
<body>

<iframe src="france.html" title="test" name="test">

</body>
</html>

iframe 页面:

<html>
<head>
<title>France</title>
</head>
<body>

<p>The Capital of France is: Paris</p>

</body>
</html>

我的 cURL 脚本:

<?php>

// 1. initialize

$ch = curl_init();

// 2. The URL containing the iframe

$url = "http://localhost/test/index.html";

// 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("'The Capital of France is:(.*?). </p>'si", $output, $match);
if($match) 

// 7. Display the scraped string 

echo "The Capital of France is: ".$match[1];

?>

结果 = 没有!

谁能帮我找出法国的首都?! ;)

我需要以下示例:

  1. 解析/抓取 iframe url
  2. 卷曲网址(就像我对 index.html 页面所做的那样)
  3. 解析字符串“Paris”

谢谢!

【问题讨论】:

  • 这不是 cURL 脚本,而是 PHP 脚本。不要将它与图书馆混淆。并且不要使用正则表达式解析 HTML!
  • 我没有看到您正在加载 iframe 的部分。您首先必须为任何 iframe 抓取索引页面,然后加载并抓取其中的每一个。 (ps 根据this question,您应该使用DOMDocument->loadHTML() 使用PHP 而非正则表达式进行HTML 解析)
  • 你喜欢,接受任何答案吗?
  • 我刚刚接受了我之前问题的所有答案 - 感谢您指出这一点!

标签: php regex parsing curl scrape


【解决方案1】:

--编辑-- 您可以将页面内容加载到字符串中,将字符串解析为 iframe,然后将 iframe 源加载到另一个字符串中。

$wrapperPage = file_get_contents('http://localhost/test/index.html');

$pattern = '/\.*src=\".*\.html"\.*/';

$iframeSrc = preg_match($pattern, $wrapperPage, $matches);

if (!isset($matches[0])) {
    throw new Exception('No match found!');
}

$src = $matches[0];

$src = str_ireplace('"', '', $src);
$src = str_ireplace('src=', '', $src);
$src = trim($src);

$iframeContents = file_get_contents($src);

var_dump($iframeContents);

--原创--

努力提高接受率(接受以前回答的问题的答案)。

您设置 curl 处理程序的 url 是包装 i-frame 的文件,尝试将其设置为 iframe 的 url:

$url = "http://localhost/test/france.html";

【讨论】:

  • 我想主要的问题是我不知道如何抓取 iframe 的链接然后获取然后抓取它!任何示例将不胜感激。
  • 当我卷曲 iframe 页面 (france.html) 时,一切正常。我需要一种方法首先将它指向 index.html - 所以我需要做一个“卷曲中的卷曲”
  • 现在试一试,但遇到:警告:preg_match() [function.preg-match]:编译失败:在 /Applications/XAMPP/xamppfiles/htdocs/curl 中的偏移量 10 处没有可重复的内容/1197846/w3.php 第 7 行致命错误:未捕获的异常“异常”,消息“未找到匹配项!”在 /Applications/XAMPP/xamppfiles/htdocs/curl/1197846/w3.php:10 堆栈跟踪:#0 {main} 在 /Applications/XAMPP/xamppfiles/htdocs/curl/1197846/w3.php 第 10 行抛出跨度>
  • @Dri:试试我的代码 file_get_contents 代替你的 curl 调用。在这种情况下可能不需要卷曲。根据 PHP 文档,file_get_contents 可以读取远程文件的内容:us2.php.net/file_get_contents
  • @Dri:var_dump($wrapperPage)初始化后试试看,看看有没有内容。
【解决方案2】:

请注意,有时由于各种原因,无法在其自己的服务器上下文之外读取 iframe curl,并且直接查看 curl 会引发某种类型的“无法直接或从外部读取”错误消息。

在这些情况下,您可以使用 curl_setopt($ch, CURLOPT_REFERER, $fullpageurl); (如果您在 php 中并使用 curl_exec 阅读文本)然后 curl_exec 认为 iframe 在原始页面中,您可以阅读源代码。

因此,如果出于某种原因无法在包含它作为 iframe 的较大页面的上下文之外读取 france.html,您仍然可以使用上述方法获取源代码,使用 CURLOPT_REFERER 并设置主页(测试/索引.html 在原始问题中)作为推荐人。

【讨论】:

  • 或者只设置 CURLOPT_AUTOREFERER
【解决方案3】:

要回答您的 问题,您的模式与输入文本不匹配:

          <p>The Capitol of France is: Paris</p>

你在结束段落标签之前有一个额外的空格,它永远不会匹配:

preg_match("'The Capitol of France is:(.*?). </p>'si"

您应该在捕获组之前有空格,然后删除多余的.

preg_match("'The Capitol of France is: (.*?)</p>'si"

要在两个位置中的任何一个使用可选空间,请改用\s*

preg_match("'The Capitol of France is:\s*(.*?)\s*</p>'si"

您还可以使捕获组只匹配带有(\w+) 的字母,以更具体。

【讨论】:

    猜你喜欢
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 2010-10-29
    相关资源
    最近更新 更多