php scrape: 只回显第三个词

php scrape: echo the third word only

所以我正在试验 php 抓取的可能性,我想更精确地了解正在回显的数据。下面的代码实现了这个输出:DJIA All Time, Record-High Close: June 14, 2017 (21,374.56)

<?php

// weather forecast los angeles
$doc = new DOMDocument;

// We don't want to bother with white spaces
$doc->preserveWhiteSpace = false;


$doc->strictErrorChecking = false;
$doc->recover = true;

$doc->loadHTMLFile('https://finance.yahoo.com/quote/%5EGSPC?p=%5EGSPC');

$xpath = new DOMXPath($doc);

$query = "//span[@class='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)']";

$entries = $xpath->query($query);
foreach ($entries as $entry) {
echo trim($entry->textContent);  // use `trim` to eliminate spaces
}
?>

有没有可能有人知道我可以指定在哪里只输出在抓取过程中出现的第三个 word/value。所以在上面的例子中,只有 "Time" 最终会得到回显。当我说 word/value 时,我想我将其识别为有一个“”空白 space。

这可能吗?抱歉,我缺乏 php 技能,对此很陌生。如果我能够在正在回显的内容中得到这个精确的 hwoever,那将带来很多可能性。

最好的,-威尔逊

可以使用 explode() php 函数,该函数用于将字符串转换为数组。

$entries = $xpath->query($query);
foreach ($entries as $entry) {
     $result = trim($entry->textContent); 
     $ret_ = explode(' ', $result);
    echo $ret_[2];

}

Another one

<?php

$text = "DJIA All Time, Record-High Close: June 14, 2017 (21,374.56)";

$text_array = explode(' ', $text);

 print_r($text_array);
 echo $text_array[2];
?>