DOMXPath object value omitted

做~自己de王妃 提交于 2019-12-12 03:55:39

问题


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);

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


回答1:


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')]");

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));
}

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



来源:https://stackoverflow.com/questions/40690627/domxpath-object-value-omitted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!