【问题标题】:Is there a (PHP) Web Scraping Framework That Uses Curl Instead of file_get_contents?是否有使用 Curl 而不是 file_get_contents 的 (PHP) Web Scraping Framework?
【发布时间】:2012-01-13 16:00:02
【问题描述】:

我正要尝试使用简单的 HTML DOM 框架进行抓取:http://simplehtmldom.sourceforge.net/,但出于安全原因,file_get_contents 在服务器配置中被禁用。

我现在需要找到一个使用 Curl 的类似框架 - 有人知道吗?

尝试运行斜线点示例时收到的错误消息是:

Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in /var/www/vhosts/domain.com/httpdocs/crawlfeed/simple_html_dom.php on line 70

【问题讨论】:

  • 你不能只是 cURL 文件然后将文本字符串加载到 SimpleHTMLDOM 中吗?
  • 您不必将 file_get_contents 与 simplehtml 一起使用。您可以使用 curl 自己获取 html 并将结果直接提供给 simplehtml。
  • 您也可以单独执行 curl 请求,然后传入字符串。 $dom = str_get_html(curl($url)->returntransfer(1)->exec());

标签: php web-scraping


【解决方案1】:

只需使用 cURL 拉下页面,然后将字符串加载到 SimpleHTMLDOM:

$ch = curl_init('http://theurl.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$htmlStr = curl_exec($ch);
curl_close($ch);

$html = new simple_html_dom();

// Load HTML from a string
$html->load($htmlStr);

【讨论】:

  • 那不是借口..我以前从未使用过它...我 KID,我 KID ;-)
【解决方案2】:

如果你有 PHP 5.3(你应该,因为不再支持 PHP 5.2)我完全推荐你Goutte

这是一种新的,它只是一个包含在您的项目中的 .phar。 HTTP 部分由 Http Zend 和一个套接字处理。而且您拥有强大的 BrowserKit 和 DomCrawler Symfony 组件来帮助您从 HTML 中提取信息(没有正则表达式,没有 xpath)。

【讨论】:

    【解决方案3】:

    只需使用 cURL 获取 HTML 代码,然后使用 XPATH 或正则表达式解析 html 代码。使用 XPATH 是一个好主意,因为它是一种专门用于解析您想要使用的 XML 或 (X)HTML 的语言。

    这里有一个很好的例子:http://www.2basetechnologies.com/screen-scraping-with-xpath-in-php

    【讨论】:

      猜你喜欢
      • 2012-01-22
      • 1970-01-01
      • 2013-11-13
      • 2016-06-23
      • 2016-09-05
      • 1970-01-01
      • 2021-10-20
      • 1970-01-01
      • 2012-02-19
      相关资源
      最近更新 更多