【发布时间】: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];
?>
结果 = 没有!
谁能帮我找出法国的首都?! ;)
我需要以下示例:
- 解析/抓取 iframe url
- 卷曲网址(就像我对 index.html 页面所做的那样)
- 解析字符串“Paris”
谢谢!
【问题讨论】:
-
这不是 cURL 脚本,而是 PHP 脚本。不要将它与图书馆混淆。并且不要使用正则表达式解析 HTML!
-
我没有看到您正在加载 iframe 的部分。您首先必须为任何 iframe 抓取索引页面,然后加载并抓取其中的每一个。 (ps 根据this question,您应该使用DOMDocument->loadHTML() 使用PHP 而非正则表达式进行HTML 解析)
-
你喜欢,接受任何答案吗?
-
我刚刚接受了我之前问题的所有答案 - 感谢您指出这一点!
标签: php regex parsing curl scrape