Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用xpathquery获取完整的类名?_Xpath_Web Scraping_Html Parsing_Domdocument_Classname - Fatal编程技术网

如何使用xpathquery获取完整的类名?

如何使用xpathquery获取完整的类名?,xpath,web-scraping,html-parsing,domdocument,classname,Xpath,Web Scraping,Html Parsing,Domdocument,Classname,我正在解析一个HTML文档,我需要一个div的类名。我知道类名的一部分(永远不会更改),但我需要完整的类名 以下是我使用的代码: $doc = new DOMDocument; $doc->loadHTMLFile('http://some_website.com'); $xpath = new DOMXPath($doc); $classname_of_the_div=$xpath->query('//div[@class="part_of_the_class_name_that_

我正在解析一个HTML文档,我需要一个div的类名。我知道类名的一部分(永远不会更改),但我需要完整的类名

以下是我使用的代码:

$doc = new DOMDocument;
$doc->loadHTMLFile('http://some_website.com');
$xpath = new DOMXPath($doc);
$classname_of_the_div=$xpath->query('//div[@class="part_of_the_class_name_that_never_changes"]');
当I
var\u dump()
分区的
$classname\u
和分区的
$classname\u->item(0)
时,结果是:

object(DOMNodeList)#3 (1) { ["length"]=> int(0) } 
NULL
我知道,
$classname\u of theu-div=$xpath->evaluate(/string(//div[@class=“part\u of theu-class\u-name\u-that\u-event\u-changes”])
提供了div的内容,但是如何获得完整的类名呢

注意:类名的一部分与类名的其余部分用空格隔开,因此它实际上不是类的一部分。div只有几个类


我的意思是div有几个类名,比如-我想通过“class2”来选择它,然后接收 完整的类字符串,包括“class1 class2 class3”

然后,一个XPath表达式,如

//div[@class="part_of_the_class_name_that_never_changes"]
将永远不会产生结果,除非特定的
div
元素只有一个类,即“永不更改”的类。这是因为上面的XPath表达式表示:

选择具有字符串值为的
class
属性的
div
元素 完全对应于“类名称的一部分,永远不会改变”


但想象一下以下情况:

<div class="part_of_the_class_name_that_never_changes other_class1 other_class2"/>
该表达的意思是:

查找具有
class
属性的
div
元素,该属性的字符串 值包含字符串 “part_of_the_class_name_the_event_change”并返回属性 价值观


如果说您只知道类名的一部分,您的意思是您只知道这个特定的
div
class
属性值的一部分吗?我的意思是div有几个类名,比如-我想通过“class2”来选择它,并接收完整的类字符串,包括“class1 class2 class3”!非常感谢你!使用\u div=$xpath->query('//div[包含(@class,“从未更改的\u类名的一部分”)]/@class)的$classname\u;_div->item(0)->值的$classname_返回了我需要的字符串
//div[contains(@class,'part_of_the_class_name_that_never_changes')]/@class