源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

php使用simple_html_dom解析HTML示例

  • 时间:2021-07-06 05:57 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:php使用simple_html_dom解析HTML示例
本文实例讲述了php使用simple_html_dom解析HTML的方法。分享给大家供大家参考,具体如下: 今天写了两个爬虫, 一个使用Python, 一个使用PHP, 说实在, 两个实现的方式都很方便. 下面说一下使用simple_html_dom这个开源类来解析HTML代码: [b]1. 下载simple_html_dom:[/b] [url=http://sourceforge.net/projects/simplehtmldom/files/]http://sourceforge.net/projects/simplehtmldom/files/[/url] 或者点击此处[url=http://xiazai.jb51.net/201607/yuanma/simple-html-dom_1_5(jb51.net).rar]本站下载[/url]。 [b]2. 用法示例:[/b]
<?php
// example of how to use basic selector to retrieve HTML contents
include('../simple_html_dom.php');
// get DOM from URL or file
$html = file_get_html('http://www.google.com/');
// find all link
foreach($html->find('a') as $e) 
  echo $e->href . '<br>';
// find all image
foreach($html->find('img') as $e)
  echo $e->src . '<br>';
// find all image with full tag
foreach($html->find('img') as $e)
  echo $e->outertext . '<br>';
// find all div tags with id=gbar
foreach($html->find('div#gbar') as $e)
  echo $e->innertext . '<br>';
// find all span tags with class=gb1
foreach($html->find('span.gb1') as $e)
  echo $e->outertext . '<br>';
// find all td tags with attribite align=center
foreach($html->find('td[align=center]') as $e)
  echo $e->innertext . '<br>';
// extract text from table
echo $html->find('td[align="center"]', 1)->plaintext.'<br><hr>';
// extract text from HTML
echo $html->plaintext;
?>

更多关于PHP相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/459.htm]PHP编码与转码操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/43.htm]php面向对象程序设计入门教程[/url]》、《[url=http://www.1sucai.cn/Special/630.htm]PHP数学运算技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/623.htm]PHP数组(Array)操作技巧大全[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》、《[url=http://www.1sucai.cn/Special/614.htm]PHP数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/111.htm]php程序设计算法总结[/url]》、《[url=http://www.1sucai.cn/Special/180.htm]php正则表达式用法总结[/url]》及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》 希望本文所述对大家PHP程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部