在laravel中使用Symfony的Crawler组件分析HTML
作者:mrr 时间:2023-11-17 18:54:07
Crawler全名是DomCrawler,是Symfony框架的组件。令人发指的是DomCrawler的没有中文文档,Symfony也没有翻译该部分,所以使用DomCrawler开发只能一点一点摸索,现将使用过程中的经验总结。
首先是安装
composer require symfony/dom-crawler
composer require symfony/css-selector
css-seelctor 是 css选择器,用css选择节点时一些函数会用到
手册里面使用的例子是
use Symfony\Component\DomCrawler\Crawler;
$html = <<<‘HTML‘
Hello World!
Hello Crawler!
HTML;
$crawler = new Crawler($html);
foreach ($crawler as $domElement)
{
var_dump($domElement->nodeName);
}
打印的结果是
string ‘html‘ (length=4)
因为这段html代码的nodeName就是html,英语不好,开始使用的时候还以为程序错了。。。
实际使用过程,如果new Crawler($html)会出现乱码问题,应该是与页面编码有关,所以可以采用下面的方式,先初始化crawler,然后添加node
$crawler = new Crawler();
$crawler->addHtmlContent($html);
addHtmlContent的第二个参数是charset,默认是utf-8。
其他例子可以参考官方文档,http://symfony.com/doc/current/components/dom_crawler.html
记录一下工作中一点点试出来的用法
filterXPath(string $xpath) 方法,按照手册上的说法,该方法的参数是$xpath,经常用的是p,div等块。
echo $crawler->filterXPath(‘//body/p‘)->text();
echo $crawler->filterXPath(‘//body/p‘)->last()->text();
输出是第一个和下一个p标签块的文本
var_dump($crawler->filterXPath(‘//body‘)->html());
输出body内的html
foreach ($crawler->filterXPath(‘//body/p‘) as $i => $node) {
$c = new Crawler($node);
echo $c->filter(‘p‘)->text();
}
filterXPath获得的是DOMElement块的数组,每个DOMElement块可以使用新的crawler对象继续解析
$nodeValues =
$crawler->filterXPath(‘//body/p‘)->each(function (Crawler $node, $i) {
return $node->text();
});
crawler提供了each循环,使用闭包函数简化代码,不过注意的是,这种写法$nodeValues得到的是数组,需要进一步处理。
其他用法
echo $crawler->filterXPath(‘//body/p‘)->attr(‘class‘);
可以获得第一个p标签对应class属性的值“message”
$crawler->filterXPath(‘//div[@class="样式"]‘)->filter(‘a‘)->attr(‘href‘);
$crawler->filterXPath(‘//div[@class="样式"]‘)->filter(‘a>img‘)->extract(array(‘alt‘, ‘href‘))
以上是获得标签属性的一些方法
filter和filterXPath不同,手册上写的是css选择器,不太明白,我理解是div这种XPath节点包含的元素,具体情况还需要在实际开发中去尝试。
总的来说感觉DomCrawler要比simple html dom好用一些,可能是我用的比较浅显。
上述只是Crawler的基本功能,更过用法请查阅symfony手册关于Crawler部分的函数
http://api.symfony.com/3.2/Symfony/Component/DomCrawler/Crawler.html
Crawler主要问题还是示例太少,函数手册里面没有使用实例,只能在实际使用中去摸索。。。。
symfony关于DomCrawler的文档,里面有少数例子
http://symfony.com/doc/current/components/dom_crawler.html
以上所述是小编给大家介绍的在laravel中使用Symfony的Crawler组件分析HTML,希望对大家有所帮助.
来源:http://www.tuicool.com/articles/EjYRVf3
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
关于Python的Thread线程模块详解
![](https://img.aspxhome.com/file/2023/5/118355_0s.png)
mysql 8.0.18 安装配置优化教程
![](https://img.aspxhome.com/file/2023/3/85813_0s.png)
Python深度学习线性代数示例详解
![](https://img.aspxhome.com/file/2023/8/91828_0s.png)
一篇文章让你搞清楚JavaScript事件循环
![](https://img.aspxhome.com/file/2023/5/136045_0s.png)
python 实现简单的FTP程序
![](https://img.aspxhome.com/file/2023/2/85382_0s.jpg)
详解小白之KMP算法及python实现
![](https://img.aspxhome.com/file/2023/6/128776_0s.png)
解决python中os.system调用exe文件的问题
![](https://img.aspxhome.com/file/2023/0/63190_0s.png)
使用python+Flask实现日志在web网页实时更新显示
![](https://img.aspxhome.com/file/2023/6/104816_0s.jpg)
Python模块结构与布局操作方法实例分析
设计英文网站要注意的问题
Python下的常用下载安装工具pip的安装方法
详解vue数组遍历方法forEach和map的原理解析和实际应用
Python 网络编程之TCP客户端/服务端功能示例【基于socket套接字】
Python全栈之列表数据类型详解
使用PHP 5.0创建图形的巧妙方法
vue3缓存页面keep-alive及路由统一处理详解
![](https://img.aspxhome.com/file/2023/4/130234_0s.gif)
CSS3 + HTML5 实现未来 Web 设计
![](https://img.aspxhome.com/file/UploadPic/20101/25/232021rjs-74s.png)
Python Web开发模板引擎优缺点总结
网址导航的组织方法
python select.select模块通信全过程解析
![](https://img.aspxhome.com/file/2023/3/101493_0s.jpg)