显示 DOMNodeList 对象中的空元素

Show empty elements in DOMNodeList object

我的代码需要一些帮助,因为我在解析 html 源代码中标签中的元素时遇到问题。当我尝试这个时:

 $doc = new DOMDocument();
 $doc->preserveWhiteSpace = false;
 $doc->loadHTML($html);
 $get_time = $doc->getElementById('date-time');

我将得到空输出。我试过像这样使用 datetime

$get_time = $doc->getElementsByTagName('date');

$get_time = $doc->getElementsByTagName('time');

当我尝试时,没有任何效果,因为我会得到这样的 return 输出:

DOMNodeList Object ( [length] => 0 )

这里是 html 代码:

["<a style='width: 149px;' data-time='6:00 am' </a><a style='width: 149px;' data-time='6:30 am' 
</a><a style='width: 149px; data-time='7:00 am' </a><a style='width: 149px; data-time='7:30 am' 
</a><a style='width: 99px; data-time='7:00 am' </a>"]

这是我想要实现的目标:

6:00 am
6:30 am
7:00 am
7:30 am
8:00 am

完整代码如下:

$url = 'http://example.com/GS?cid=1234'
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_USERAGENT => '',
    CURLOPT_TIMEOUT => 30,
    CURLOPT_CONNECTTIMEOUT => 30,
    CURLOPT_HEADER => false,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_MAXREDIRS => 5,
    CURLOPT_URL => $url,
 ));

 $html = curl_exec($curl);
 curl_close($curl);

 $doc = new DOMDocument();
 $doc->preserveWhiteSpace = false;
 $doc->loadHTML($html);
 $get_time = $doc->getElementsByTagName('date-time');

 foreach($get_timeas $time)
 {
    echo $time;
 }

我真的不明白为什么当我使用函数 getElementsByTagName 时它没有工作,它应该工作但没有工作。你能告诉我一个例子,我如何解析标签 date-timeusing with domdocument?

编辑:

这就是我在使用 var_dumpprint_r 时得到的结果:

array(1) { [0]=> string(10830) "
UEFA Europa League Highlights
Hoogtepunten van alle wedstrijden in de UEFA Europa League.7.0
Fox Sports doc
Dejan Curovic - DJ Superstar
Samenvatting
Samenvatting
Fox Sports doc
Alleen onder de Lat
Eredivisie Highlights

除非您打开 html 源,否则不会显示日期时间。

假设这是 json:

$url = 'http://example.com/GS?cid=1234'
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_USERAGENT => '',
    CURLOPT_TIMEOUT => 30,
    CURLOPT_CONNECTTIMEOUT => 30,
    CURLOPT_HEADER => false,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_MAXREDIRS => 5,
    CURLOPT_URL => $url,
 ));

 $result = curl_exec($curl);
 curl_close($curl);

 $html = json_decode($result);
 $doc = new DOMDocument();
 $doc->preserveWhiteSpace = false;
 $doc->loadHTML($html[0]);
 $get_time = $doc->getElementsByTagName('date-time');

 foreach($get_timeas $time)
 {
    echo $time;
 }

您的示例 HTML 格式错误:它缺少 <a> 右括号和样式属性中的一些引号。除此之外,如果 html 正确,您可以使用 Xpath 表达式查询 HTML

$html = '
<a style="width: 149px;" data-time="6:00 am"></a>
<a style="width: 149px;" data-time="6:30 am"></a>
<a style="width: 149px;" data-time="7:00 am"></a>
<a style="width: 149px;" data-time="7:30 am"></a>
<a style="width: 99px;" data-time="7:00 am"></a>';

$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;

// here the document is completed with the missed html tag (body etc.)
// print $doc->saveHTML() to analyze the document
$doc->loadHTML($html);

// query for attributes
$xpath = new DOMXPath($doc);
$res = $xpath->query("//@data-time");

// map attributes node values
$map = array();
foreach($res as $node) {
   $map[] = $node->value;
}

// expected results
var_dump($map);

请注意,如果提供的 HTML 不是有效的 html,DOMDocument 库不会像浏览器那样 clean/correct HTML,因此您可以有一些解析错误或一些意外行为。

如果在您的示例中方括号包含在您需要转换结果的结果中(如果它是有效的 json 您可以解析它)

XPath 语法示例
https://msdn.microsoft.com/en-us/library/ms256122(v=vs.110).aspx

片段
https://repl.it/repls/ValuableMundaneConnection