可以';t选择链接


Can't select link

我正在尝试抓取每个.row的href。最终,我想点击链接并访问它链接的DOM,但我无法获得link对象或href属性。。

不确定a属性中没有任何文本这一事实是否是一个问题,但这是我必须处理的DOM。

帮助?

<?php require 'vendor/autoload.php';
use Symfony'Component'DomCrawler'Crawler;
$html = <<<'HTML'
<!doctype html>
<html>
  <body>
    <div class="content">
      <p class="row"><a href="/uri1"></a></p> 
      <p class="row"><a href="/uri2"></a></p> 
      <p class="row"><a href="/uri3"></a></p> 
    </div>
  </body>
<html>
HTML;
$dom = new Crawler($html);
$content = $dom->filter('.row');
$rows = [];
foreach ($content as $element)
{
    $node = new Crawler($element);
    $link = $node->filter('a');
    echo $link->html(); // Empty?
    try 
    {
        $link = $node->selectLink('')->link();
        echo $link->getUri();
    } 
    catch (Exception $ex) 
    {
        // Throws: Current URI must be an absolute URL ("").Current URI must be 
        // an absolute URL ("").Current URI must be an absolute URL ("").
        echo $ex->getMessage();
    }
}

我使用xpath来用DomCrawler过滤DOM元素,因为我喜欢对过滤内容有更多的控制权。下面的代码应该回显html中的url。

$crawler = new Crawler($html);
$crawler->filterXPath("//p[@class='row']")->each(function (Crawler $node, $i) {
$url = $node->filterXPath("//a/@href")->text();
echo $url;
}