省略DOMXPath对象值 [英] DOMXPath object value omitted

查看:68
本文介绍了省略DOMXPath对象值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了很多stackoverflow问题,我正在使用此代码,但是我不知道为什么这行不通。

这是代码。

I read many stackoverflow question and I'm using this code but I don't know why this is not work.
Here is a code.

$url = 'http://m.cricbuzz.com/cricket-schedule';
$source = file_get_contents($url);

$doc = new DOMDocument;
@$doc->loadHTML($source);

$xpath = new DOMXPath($doc);
$classname = "list-group";
$events = $xpath->query("//*[contains(@class, '$classname')]");

var_dump($xpath);

能否请您检查一下为什么这实际上不起作用我想从<$ c $中获取数据c> list-group

Can you please check it why this is not working actually I want to get data from list-group

推荐答案

代码正确。它将具有指定类属性值的DOM节点列表正确地提取到 $ events 变量中:

The code is correct. It correctly fetches a list of DOM nodes having the specified class attribute value into the $events variable:

$events = $xpath->query("//*[contains(@class, '$classname')]");

这是 DOMNodeList 的实例。接下来,您应该遍历列表,并从 $ events 中获取所需的数据。例如,如果您需要节点的外部HTML,请使用以下内容:

which is an instance of DOMNodeList. Next you should iterate the list and fetch the data you need from $events. For example, if you need the outer HTML for the nodes, use something like this:

foreach ($events as $e) {
  printf("<<<<<\n%s\n>>>>>\n", $e->ownerDocument->saveXML($e));
}

PS:我将重命名 $ events $ elements

P.S.: I would rename $events to $elements.

这篇关于省略DOMXPath对象值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆