crawler的简单运用(类似phpquery)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wt1286331074/article/details/88422343

参考:http://www.php.cn/php-weizijiaocheng-376167.html (中文)
https://symfony.com/doc/current/components/dom_crawler.html (官方英文)
因为是项目中运用,首先composer下composer require symfony/dom-crawler然后use写代码

$html = <<<HTML
        <body>
        Hello World!
        Hello Crawler!
<p class="p-1">我是p标签11</p>
<p class="p-2">我是p标签22</p>

<p class="p-3">
    <a alt="first" href="www.first.com"><img src="www.img222.com" alt="tu1">独一</a>
    <a ><img src="www.img.com" alt="tu2"></a>
</p>

</body>
HTML;
        $crawler = new Crawler($html);
        echo $crawler->filterXPath('//body/p')->text();// 获得里面的dom
        // 获得内容里面重复的标签内容
        foreach ($crawler->filterXPath('//body/p') as $i=>$node){
            $c = new Crawler($node);
            echo $c->filter('p')->text();
        }
        $nodeValues = $crawler->filterXPath('//body/p')->each(function (Crawler $node,$i){
            return $node->text();
        });
        // 获得第二个p标签的class属性值
        echo $crawler->filterXPath('//body/p')->last()->attr('class');

        // 根据特定的class属性搜
       echo $crawler->filterXPath('//p[@class="p-2"]')->text();
        // 寻找,抽取属性
         $dataArr = $crawler->filterXPath('//p[@class="p-3"]')->filter('a>img')->extract(array('alt'));

猜你喜欢

转载自blog.csdn.net/wt1286331074/article/details/88422343