【问题标题】:PHP Simple HTML DOMPHP 简单的 HTML DOM
【发布时间】:2013-11-25 18:18:46
【问题描述】:

我正在尝试从使用 PHP CURL 检索的 html 源访问 H1 标记。

HTML:http://pastebin.com/4ubAhS3E

我正在尝试访问最后一行(第 63 行):<h1>Your referrals (5 complete / 0 pending / 9999 total)</h1>

这是php simple html dom正在使用的部分代码

$result1 = curl_exec ($ch1);
curl_close($ch1);   
echo $result1;


$html = str_get_html($result1);

$main = $html->find('h1', 3); 
foreach($main as $element) 
   echo $element->innertext . '<br>';

但它只是带回了整个 HTML

【问题讨论】:

  • -&gt;find() 仅在您未指定第二个参数时才返回一个数组。如果你这样做,它会返回一个 dom 节点。

标签: php dom


【解决方案1】:

使用 DOMDocument 很容易,你可以获取元素内的文本内容:

$result1 = curl_exec ($ch1);
curl_close($ch1);   
echo $result1;

$dom = new DOMDocument();
$dom->loadHTML($result1);
$xpath = new DOMXpath($dom);

echo $xpath->evaluate('string(//section[@id="your_referrals"]/h1)');

【讨论】:

  • 这太完美了,我绕着圈子试图做到这一点。感谢您抽出宝贵时间。
猜你喜欢
  • 2018-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多