gpt4 book ai didi

PHP 和 xPath 问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:00:18 24 4
gpt4 key购买 nike

我正在使用 PHP 和 xPath 抓取我拥有的网站(只是抓取 html 而不是进入服务器)但我收到此错误:

Catchable fatal error: Object of class DOMNodeList could not be converted to string in C:\wamp\www\crawler.php on line 46

我已经尝试只回显那一行以查看我得到了什么,但我也会得到同样的错误我也尝试谷歌搜索错误但我最终在 php 文档中找到并发现我的示例是与 php 文档中的完全一样,只是我使用的是 HTML 而不是 XML...所以我不知道出了什么问题...这是我的代码...

<?php
$html = file_get_contents('http://miurl.com/mipagina#0');
// create document object model
$dom = new DOMDocument();
// load html into document object model
@$dom->loadHTML($html);
// create domxpath instance
$xPath = new DOMXPath($dom);
// get all elements with a particular id and then loop through and print the href attribute
$elements = $xPath->query("//*[@class='nombrecomplejo']");
if ($elements != null) {
foreach ($elements as $e) {
echo parse_str($e);
}
}
?>

编辑

实际上是的,很抱歉,当我评论其他内容时,那条线是为了测试......我在这里删除了它,但仍然有错误。

最佳答案

根据documentation , "$elements != null"检查是不必要的。 DOMXPath::query() 将始终返回一个 DOMNodeList,尽管它的长度可能为零,这不会混淆 foreach 循环.

另外,请注意使用 nodeValue 属性来获取元素的文本表示:

$elements = $xPath->query("//*[@class='nombrecomplejo']");

foreach ($elements as $e) {
echo $e->nodeValue;
}

你得到这个错误的原因是你不能给 parse_str() 输入字符串以外的任何东西。 ,您尝试传入一个 DOMElement

关于PHP 和 xPath 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/548744/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com