【问题标题】:Extracting HTML Data with Curl / PHP使用 Curl / PHP 提取 HTML 数据
【发布时间】:2011-04-21 03:18:56
【问题描述】:

我在 PHP 中有以下代码

$ch = curl_init("http://blog.com");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);      
curl_close($ch);
echo $output;

我正在尝试导入<div id="content"></div> 之间的代码块我想知道提取此代码的最佳方法。

【问题讨论】:

标签: php string wordpress curl cross-domain


【解决方案1】:

DOM 将是最好的方法。这是详细的文档:http://php.net/manual/en/book.dom.php

【讨论】:

  • 这个答案并没有完全回答这个问题。使用它创建 DOM 不会显示 DOM 创建过程。
【解决方案2】:

使用 curl,$output 将是一个字符串,所以使用它创建一个 DOM:

$html = str_get_html($content);

然后,获取内容:

$content = $html->find('#content');

【讨论】:

  • 感谢您的回答,但它不适用于我的,当我尝试在 str_get_html 中传递字符串时,它会给我很多代码
猜你喜欢
  • 1970-01-01
  • 2014-07-21
  • 2011-04-08
  • 2015-06-09
  • 1970-01-01
  • 2015-11-22
  • 2011-07-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多