我正在尝试使用simpleXML从网站上获取图像,并且我收到一条PHP错误,说我正在尝试调用xpath()
非对象上的成员函数.
下面是我试图用来获取图像源标记的行:
$xpath = '/html/body/div/div/div[5]/div/div/div[2]/div/div[2]/img'; $html = new DOMDocument(); @$html->loadHTMLFile($target_URL); $xml = simplexml_import_dom($html); $source_image = $xml->xpath($xpath); $source_image = $source_image[0]['src'];
我究竟做错了什么?很明显倒数第二行有问题,但我不确定它是什么.
尝试此代码首先确保正确解析文档.
$xpath = '/html/body/div/div/div[5]/div/div/div[2]/div/div[2]/img'; $html = new DOMDocument(); @$html->loadHTMLFile($target_URL); $xml = simplexml_import_dom($html); if (!$xml) { echo 'Error while parsing the document'; exit; } $source_image = $xml->xpath($xpath); $source_image = $source_image[0]['src'];