2017-04-20 49 views
0

我正在用PHP抓取一个网站。我已经刮掉了所有必需的数据,但我无法刮去span标签文本。如何在PHP中获取Span标签文本?

预期输出:Apr 20,2017

<span title="" data-toggle="tooltip" data-original-title="Posted On"> 
             <i class="calendar rz-calendar"></i>Apr 20, 2017 
            </span> 
$html = file_get_contents($url); 
libxml_use_internal_errors(true); 
$doc = new DOMDocument; 
$doc->loadHTML($html); 
$xpath = new DOMXpath($doc); 

$node = $xpath->query('//span[@data-original-title="Posted ON"]'); 

回答

1

问题在你的代码是//span[@data-original-title="Posted ON"]ON是不是在首都就是这个样子On

Try this code snippet here

<?php 
ini_set('display_errors', 1); 


$doc = new DOMDocument; 
$doc->loadHTML('<span title="" data-toggle="tooltip" data-original-title="Posted On"> 
             <i class="calendar rz-calendar"></i>Apr 20, 2017 
            </span>'); 
$xpath = new DOMXpath($doc); 
$nodeList = $xpath->query('//span[@data-original-title="Posted On"]'); 
foreach($nodeList as $node) 
{ 
    echo trim($node->textContent); 
} 
+0

我试了一下,它不是加工..! –

+0

你检查了我的帖子演示吗?也可以分享你的HTML以及 –

+0

这里是脚本..! <?php ini_set('display_errors',1); $ html = file_get_contents('https://www.rozee.pk/job/jsearch/q/java'); libxml_use_internal_errors(true); \t $ doc = new DOMDocument; \t $ doc-> loadHTML($ html); \t $ xpath = new DOMXpath($ doc); $ nodeList = $ xpath-> query('// span [@ data-original-title =“Posted on”]'); ($ nodeList作为$ node) { echo trim($ node-> textContent); } ?> –

相关问题