Php 如何在XPath中从子项(HTML)获取属性值
首先,我寻找了我需要的所有元素,现在我试图从子元素中获取属性值——标题、url和图像——但总是出错。我做错了什么 函数getContent$value { $homepage=文件\u获取\u内容'https://www.youtube.com/results?search_query=“.$value; $doc=新文档; libxml\u use\u internal\u errorsTRUE;//禁用libxml错误 //检查是否实际返回了任何html 如果!空$homepage{ //装载 $doc->loadHTML$主页; //删除讨厌的HTML的错误 libxml_清除_错误; //获取DOMxPath $scriptXpath=new-DOMXPath$doc; //获取所有元素 $scriptRows=$scriptXpath->query'/*[@class=item section]/li[position>1]; $videos=数组; foreach$scriptRows作为$scriptRow{ $VideoTitle=$scriptRow->{'/div/div/div/h3/a/@title'}; $VideoUrl=https://youtube.com'.$scriptRow->{'/div/div/div[2]/h3/a/@href'}; $VideoImg=$scriptRow->{'/div/div/div[1]/a/div/span/img/@src'}; //添加到一系列视频的末尾 $videos[]=[ “title”=>$VideoTitle, “url”=>$VideoUrl, “图像”=>$VideoImg, ]; }Php 如何在XPath中从子项(HTML)获取属性值,php,html,xpath,Php,Html,Xpath,首先,我寻找了我需要的所有元素,现在我试图从子元素中获取属性值——标题、url和图像——但总是出错。我做错了什么 函数getContent$value { $homepage=文件\u获取\u内容'https://www.youtube.com/results?search_query=“.$value; $doc=新文档; libxml\u use\u internal\u errorsTRUE;//禁用libxml错误 //检查是否实际返回了任何html 如果!空$homepage{ //
} 通过使用“getAttribute”解决 $scriptRows=$scriptXpath->query'/*[@class=item section]/li[position>1]/div/div/div/h3/a'; foreach$scriptRows作为$scriptRow{ $VideoTitle=$scriptRow->getAttributetitle; $VideoUrl=https://youtube.com'.$scriptRow->getAttributehref; $videos[]=[ “title”=>$VideoTitle, “url”=>$VideoUrl, //“图像”=>$VideoImg, ]; } $scriptRows=$scriptXpath->query'/*[@class=item section]/li[position>1]/div/div/div[1]/a/div/span/img'; foreach$scriptRows作为$scriptRow{ //添加到一系列视频的末尾 $VideoImg=$scriptRow->getAttributesrc; $videos[]=[ “图像”=>$VideoImg, ];
}请添加错误消息并更好地描述您的问题。请避免对您自己的问题发表评论,您应该对其进行编辑。请参阅:和。