PHP爬虫框架 第13章 phpQuery PHP爬虫框架 第13章 phpQuery

2024-05-09

一、介绍

phpQuery:PHP 的服务器端 jQuery,类似 jquery 的强大搜索 DOM 的能力。

phpQuery:是一个功能强大的搜索 DOM 的方法,跟 jQuery 的 $() 如出一辙,jQuery 的选择器基本上都能使用在 phpQuery上,只要把“.”变成“->”

二、下载

github

https://github.com/TobiaszCudnik/phpquery

gitee

https://gitee.com/mirrors/phpquery

三、例子

例1

require('phpQuery/phpQuery.php');
phpQuery::newDocumentFile('http://www.baidu.com/');
$menu_a = pq("a");
foreach($menu_a as $a){
   echo pq($a)->html();
}
foreach($menu_a as $a){
   echo pq($a)->attr("href");
}

例2

require('phpQuery/phpQuery.php');
//载入百度首页
$html = file_get_contents('https://www.baidu.com');
$doc = phpQuery::newDocumentHTML($html);
//获取百度首页Logo图片链接地址
$img_url = $doc->find('#lg img')->attr('src');
//通过file_get_contents()函数获取图片内容并保存到本地
$img_content = file_get_contents($img_url);
file_put_contents('baidu_logo.jpeg', $img_content);

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开微信扫一扫,即可进行扫码打赏哦

阅读 174