参考: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-&gt;filterXPath('//p[@class="p-2"]')-&gt;text();
    // 寻找,抽取属性
     $dataArr = $crawler-&gt;filterXPath('//p[@class="p-3"]')-&gt;filter('a&gt;img')-&gt;extract(array('alt'));