简体   繁体   English

PHP简单HTML DOM

[英]PHP Simple HTML DOM

I'm trying to access a H1 tag from a html source I am retrieving with PHP CURL. 我正在尝试从使用PHP CURL检索的html源访问H1标签。

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

I'm trying to access the last line (line 63): <h1>Your referrals (5 complete / 0 pending / 9999 total)</h1> 我正在尝试访问最后一行(第63行): <h1>Your referrals (5 complete / 0 pending / 9999 total)</h1>

Here is the part of the code that the php simple html dom is using 这是php简单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>';

But it's just bringing back the whole HTML 但这只是带回了整个HTML

It is easy with DOMDocument, you can fetch the text content inside an element: 使用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)');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM