您的位置:首页 > 编程语言 > PHP开发

【php网页爬虫】php抓取网页数据

2018-03-08 14:01 591 查看
插件介绍:
PHP Simple HTML DOM解析类:Simple HTML DOM parser 帮我们很好地解决了使用 php html 解析 问题。可以通过这个php类来解析html文档,对其中的html元素进行操作 (PHP5+以上版本)。



下载地址:https://github.com/samacs/simple_html_dom


使用方法:
1.引入simplehtmldom  
[php] view plain copy <?php  
// 新建一个Dom实例  
$html = new simple_html_dom();  
[php] view plain copy //一下三种加载方式按照需求选择其中一种即可  
// 从url中加载  
$html->load_file('http://www.jb51.net');  
  
// 从字符串中加载  
$html->load('<html><body>从字符串中加载html文档演示</body></html>');  
  
//从文件中加载  
$html->load_file('path/file/test.html');  

2.获取html内容
[php] view plain copy
fba5
/查找html文档中的超链接元素  
$a = $html->find('a');  
  
//查找文档中第(N)个超链接,如果没有找到则返回空数组.  
$a = $html->find('a', 0);  
  
// 查找id为main的div元素  
$main = $html->find('div[id=main]',0);  
  
// 查找所有包含有id属性的div元素  
$divs = $html->find('div[id]');  
  
// 查找所有包含有id属性的元素  
$divs = $html->find('[id]');  


还可以使用类似jQuery的选择器来查找定位元素:[php] view plain copy <?php  
// 查找id='#container'的元素  
$ret = $html->find('#container');  
  
// 找到所有class=foo的元素  
$ret = $html->find('.foo');  
  
// 查找多个html标签  
$ret = $html->find('a, img');  
  
// 还可以这样用,查找含title属性的a标签和img标签  
$ret = $html->find('a[title], img[title]');  
?>  

解析器支持对子元素的查找
[php] view plain copy <?php  
  
// 查找 ul列表中所有的li项  
$ret = $html->find('ul li');  
  
//查找 ul 列表指定class=selected的li项  
$ret = $html->find('ul li.selected');  

使用内置函数可以轻松定位元素的父元素、子元素与相邻元素
[php] view plain copy <?php  
// 返回父元素  
$e->parent;  
  
// 返回子元素数组  
$e->children;  
  
// 通过索引号返回指定子元素  
$e->children(0);  
  
// 返回第一个资源速  
$e->first_child ();  
  
// 返回最后一个子元素  
$e->last _child ();  
  
// 返回上一个相邻元素  
$e->prev_sibling ();  
  
//返回下一个相邻元素  
$e->next_sibling ();  

元素属性操作
使用简单的正则表达式来操作属性选择器。
[sql] view plain copy div[attribute] – 选择包含某属性的div元素  
div[attribute=value] – 选择所有指定值属性的div元素  
div[attribute!=value]- 选择所有非指定值属性的div元素  
div[attribute^=value] -选择所有指定值开头属性的div元素  
div[attribute$=value] 选择所有指定值结尾属性的div元素  
div[attribute*=value] -选择所有包含指定值属性的div元素  
在解析器中调用元素属性
在DOM中元素属性也是对象:
[php] view plain copy <?php  
// 本例中将$a的锚链接值赋给$link变量  
$link = $a->href;  
?>  

或者:
[php] view plain copy <?php  
$link = $html->find('a',0)->href;  
?  

每个对象都有4个基本对象属性:
[php] view plain copy tag – 返回html标签名  
innertext – 返回innerHTML  
outertext – 返回outerHTML  
plaintext – 返回html标签中的文本  

在解析器中编辑元素
编辑元素属性的用法和调用它们是类似的:
[php] view plain copy <?php  
//给$a的锚链接赋新值  
$a->href = 'http://www.jb51.net';  
  
// 删除锚链接  
$a->href = null;  
  
// 检测是否存在锚链接  
if(isset($a->href)) {  
//代码  
}  
?>  

解析器中没有专门的方法来添加、删除元素,不过可以变通一下使用:
[php] view plain copy <?php  
// 封装元素  
$e->outertext = '<div class="wrap">' . $e->outertext . '<div>';  
  
// 删除元素  
$e->outertext = '';  
  
// 添加元素  
$e->outertext = $e->outertext . '<div>foo<div>';  
  
// 插入元素  
$e->outertext = '<div>foo<div>' . $e->outertext;  
?  

保存修改后的html DOM文档也非常简单:
[php] view plain copy <?php  
$doc = $html;  
// 输出  
echo $doc;  
?>  

如何避免解析器消耗过多内存
如果php脚本占用内存太多,会导致网站停止响应等一系列严重的问题。解决的方法也很简单,在解析器加载html文档并使用完成后,记得清理掉这个对象就可以了。当然,也不要把问题看得太严重了。如果只是加载了2、3个文档,清理或不清理是没有多大区别的。当你加载了5个10个甚至更多的文档的时候,用完一个就清理一下内存绝对是对自己负责啦^_^


[php] view plain copy <?php  
$html->clear();  
?> 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: