InvalidArgumentException-当前节点列表为空. [英] InvalidArgumentException - The current node list is empty.
本文介绍了InvalidArgumentException-当前节点列表为空.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用goutte sracper抓取数据,出现类似InvalidArgumentException - The current node list is empty.
的错误,下面是我正在使用的代码
I am using goutte sracper to scrape the data , i m getting error like InvalidArgumentException - The current node list is empty.
Below is the code which i m using
$string = $crawler->filter('div#links.results')->html();
if ( empty( $string ) )
return false;
$dom = new \DOMDocument;
$state = libxml_use_internal_errors(true);
$dom->loadHTML($string);
libxml_use_internal_errors($state);
$xp = new \DOMXPath($dom);
$divNodeList = $xp->query('//div[contains(@class, "results_links_deep")]
[contains(@class, "web-result")]
/div[contains(@class, "links_main")]
[contains(@class, "links_deep")]
[contains(@class, "result__body")]');
$results = [];
if(count($divNodeList) > 0){
foreach ($divNodeList as $divNode) {
$results[] = [
trim($xp->evaluate('string(./h2/a[@class="result__a"])', $divNode)),
trim($xp->evaluate('string(.//a[@class="result__snippet"])', $divNode)),
trim($xp->evaluate('string(.//a[@class="result__url"])', $divNode))
];
}
}
我尝试使用以下解决方案
I tried using the solution as below
if ($crawler->filter('div#links.results')->count() > 0 ) {
$string = $crawler->filter('div#links.results')->html()
}
然后它开始发出另一个错误,例如DOMDocument::loadHTML(): Empty string supplied as input
then it started giving another error like DOMDocument::loadHTML(): Empty string supplied as input
有什么建议吗?
推荐答案
您的filter
没有返回任何结果.这就是为什么它崩溃了.这就是我通过添加try catch来解决此问题的方法.
Your filter
did not return any results. That is why it crashed. That's how I solved this issue, by adding a try catch.
try {
$string = $crawler->filter('div#links.results')->html()
} catch (\InvalidArgumentException $e) {
// Handle the current node list is empty..
}
这篇关于InvalidArgumentException-当前节点列表为空.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文