php采集(phpOuery用法)
2016-03-31 10:56
330 查看
一、下面简单举例:
<?php
include 'phpQuery.php';//导入phpQuery类
phpQuery::newDocumentFile('http://www.phper.org.cn');
echo pq("title")->text;//获取网页标题
echo pq("div#header")->html();//获取id为header的div的html内容
echo pq("td.tit")->html//获取class为tit的td的html内容
$artlist = pq(".news_list");
foreach($artlist as $ul){
$arr[] = pq($ul)->html();
}
print_r($arr);
$string = pq(".news_list");
foreach($string as $a){
echo pq($a)->find('a')->attr('href')."<br/>";//获取a标签的href的属性值
}
?>
二、载入文档(loading documents)
加载文档主要通过phpQuery::newDocument来进行操作,其作用是使得phpQuery可以在服务器预先读取到指定的文件或文本内容。
主要的方法包括:
phpQuery::newDocument($html,$contentType=null)
phpQuery::newDocumentFile($file,$contentType=null)
phpQuery::newDocumentHTML($html,$charset='utf-8')
phpQuery::newDocumentXHTML($html,$charset='utf-8')
phpQuery::newDocumentXML($html,$charset='utf-8')
phpQuery::newDocumentPHP($html,$contentType='null')
phpQuery::newDocumentFileHTML($file,$charset='utf-8')
phpQuery::newDocumentFileXHTML($file,$charset='utf-8')
phpQuery::newDocumentFileXML($file,$charset='utf-8')
phpQuery::newDocumentFilePHP($file,$contentType)
三、pq()函数用法
pq()函数用法是phpQuery的重点,主要分两个部分:选择器和过滤器
【选择器】
要了解phpQuery选择器的用法,建议先了解jQuery的语法
最常用的语法包括有:
pq('#id'):即以#号开头的ID选择器,用于选择器已知ID的容器所包括的内容
pd('.classname'):即以.开头的class选择器,用于选择class匹配的容器内容
pq('parent>child'):选择指定层次结构的容器内容,如:pq('.main>p')用于选择class=main容器下的所有p标签
更多的语法请参照jQuery手册
【过滤器】
主要包括::first,:last,:not,:even,:add,:eq(index),:gt(index),:lt(index),:header,:animated等,如:
pq('p:last'):用于选择最后一个P标签
pq('tr:even'):用于选择表格中偶数行
四、phpQuery连贯操作
pd()函数返回的结果是一个phpQuery对象,可以对返回结果继续进行后续的操作,例如:
pd('a')->attr('href','newVal')->removeClass('className')->html('newHtml')->...
详情查阅jQuery相关资料,用法基本一致,只需要注意与->的区别即可。
<?php
include 'phpQuery.php';//导入phpQuery类
phpQuery::newDocumentFile('http://www.phper.org.cn');
echo pq("title")->text;//获取网页标题
echo pq("div#header")->html();//获取id为header的div的html内容
echo pq("td.tit")->html//获取class为tit的td的html内容
$artlist = pq(".news_list");
foreach($artlist as $ul){
$arr[] = pq($ul)->html();
}
print_r($arr);
$string = pq(".news_list");
foreach($string as $a){
echo pq($a)->find('a')->attr('href')."<br/>";//获取a标签的href的属性值
}
?>
二、载入文档(loading documents)
加载文档主要通过phpQuery::newDocument来进行操作,其作用是使得phpQuery可以在服务器预先读取到指定的文件或文本内容。
主要的方法包括:
phpQuery::newDocument($html,$contentType=null)
phpQuery::newDocumentFile($file,$contentType=null)
phpQuery::newDocumentHTML($html,$charset='utf-8')
phpQuery::newDocumentXHTML($html,$charset='utf-8')
phpQuery::newDocumentXML($html,$charset='utf-8')
phpQuery::newDocumentPHP($html,$contentType='null')
phpQuery::newDocumentFileHTML($file,$charset='utf-8')
phpQuery::newDocumentFileXHTML($file,$charset='utf-8')
phpQuery::newDocumentFileXML($file,$charset='utf-8')
phpQuery::newDocumentFilePHP($file,$contentType)
三、pq()函数用法
pq()函数用法是phpQuery的重点,主要分两个部分:选择器和过滤器
【选择器】
要了解phpQuery选择器的用法,建议先了解jQuery的语法
最常用的语法包括有:
pq('#id'):即以#号开头的ID选择器,用于选择器已知ID的容器所包括的内容
pd('.classname'):即以.开头的class选择器,用于选择class匹配的容器内容
pq('parent>child'):选择指定层次结构的容器内容,如:pq('.main>p')用于选择class=main容器下的所有p标签
更多的语法请参照jQuery手册
【过滤器】
主要包括::first,:last,:not,:even,:add,:eq(index),:gt(index),:lt(index),:header,:animated等,如:
pq('p:last'):用于选择最后一个P标签
pq('tr:even'):用于选择表格中偶数行
四、phpQuery连贯操作
pd()函数返回的结果是一个phpQuery对象,可以对返回结果继续进行后续的操作,例如:
pd('a')->attr('href','newVal')->removeClass('className')->html('newHtml')->...
详情查阅jQuery相关资料,用法基本一致,只需要注意与->的区别即可。
相关文章推荐
- 微软 Build 2016年开发者大会发布多项功能升级
- 百度编辑器ueditor 多图上传 文件格式不允许 后台配置项返回出错 解决方法
- 232. Implement Queue using Stacks
- [poj 1019] Number Sequence 数学 想法题
- iOS开发-环信UI集成
- STM32F103RBT的TFT---UC_GUI简单移植
- UIAlertController的用法
- 【ios学习记录】-如何定制UITableView的圆角单元格
- 反编译 (dex2jar和jd_gui)
- 自增ID与GUID(UUID)
- 《iOS Human Interface Guidelines》——Slider
- iOS - UITextField输入错误的震动动画
- E QUERY [thread1] TypeError: db.foo.batchInsert is not a function :
- 微软 Build 2016年开发者大会发布多项功能升级
- UITableViewAutomaticDimension
- AIX /var/adm/wtmp: Value too large to be stored in data type.
- EasyUI datagrid表格双击操作的实现
- UISearchBar
- opencv在iOS中的使用[几个滤波]以及uiimage与iplimage互转
- ios UIScrollview UIImageView点击事件处理