DOM爬虫和当前节点列表为空-如何获取列表
DOM Crawler and The current node list is empty - how to get list
我需要有关 DOM Symfony 3.2 爬虫的帮助。
这是我的代码:
$html = file_get_contents('http://www.wakacje.pl/wczasy/peru/');
$crawler = new Crawler($html);
$crawler = $crawler->filter('#gridWithPagination > div > div')->each(function (Crawler $node) {
return $node->filter('div.desc > a');
});
foreach ($crawler as $item) {
var_dump($item->attr('href'));
}
如您所见,我想从列表中的所有实例中获取 URI 参数,但此代码仅返回第一个实例,并且还出现错误:
The current node list is empty
我错过了什么?
在此先感谢您的帮助。
不确定为什么需要过滤两次,但更直接的 CSS 选择器似乎对我有用:
use Symfony\Component\DomCrawler\Crawler;
$html = file_get_contents('http://www.wakacje.pl/wczasy/peru/');
$crawler = new Crawler($html);
$nodes = $crawler->filter('div#gridWithPagination div.desc a');
$nodes->each(function ($item) {
echo $item->attr('href').'<br>';
});
我需要有关 DOM Symfony 3.2 爬虫的帮助。 这是我的代码:
$html = file_get_contents('http://www.wakacje.pl/wczasy/peru/');
$crawler = new Crawler($html);
$crawler = $crawler->filter('#gridWithPagination > div > div')->each(function (Crawler $node) {
return $node->filter('div.desc > a');
});
foreach ($crawler as $item) {
var_dump($item->attr('href'));
}
如您所见,我想从列表中的所有实例中获取 URI 参数,但此代码仅返回第一个实例,并且还出现错误:
The current node list is empty
我错过了什么? 在此先感谢您的帮助。
不确定为什么需要过滤两次,但更直接的 CSS 选择器似乎对我有用:
use Symfony\Component\DomCrawler\Crawler;
$html = file_get_contents('http://www.wakacje.pl/wczasy/peru/');
$crawler = new Crawler($html);
$nodes = $crawler->filter('div#gridWithPagination div.desc a');
$nodes->each(function ($item) {
echo $item->attr('href').'<br>';
});