simple_html_dom使用小结

2023-05-16

简单范例

<?php

include "simple_html_dom.php" ; // Create DOM from URL or file
$html file_get_html('http://www.google.com/');

// Find all images 
foreach($html->find('img') as $element
       echo $element->src '<br>';

// Find all links 
foreach($html->find('a') as $element
       echo $element->href '<br>';


// Create DOM from URL
$html file_get_html('http://slashdot.org/');

// Find all article blocks
foreach($html->find('div.article') as $article) {
    $item['title']     = $article->find('div.title'0)->plaintext;
    $item['intro']    = $article->find('div.intro'0)->plaintext;
    $item['details'] = $article->find('div.details'0)->plaintext;
    $articles[] = $item;
}

print_r($articles);


// Create DOM from string
$html str_get_html('<div id="hello">Hello</div><div id="world">World</div>'); $html->find('div', 1)->class = 'bar';

$html->find('div[id=hello]', 0)->innertext = 'foo';

echo $html// Output: <div id="hello">foo</div><div id="world" class="bar">World</div>


3.DOM的方法
$html =  file_get_html ( 'http://www.google.com/' );        // $html 所拥有的方法如下表所示
$html->clear() ;                                                              // 调用方法
DOM  methods & properties 
Name Description
void
__construct ( [string $filename] )
Constructor, set the filename parameter will automatically load the contents, either text or file/url.
 string
plaintext
Returns the contents extracted from HTML.
void
clear ()
Clean up memory.
void
load ( string $content )
Load contents from a string.
string
save ( [string $filename] )
Dumps the internal DOM tree back into a string. If the $filename is set, result string will save to file.
void
load_file ( string $filename )
Load contents from a from a file or a URL.
void
set_callback ( string $function_name )
Set a callback function.
mixed
find ( string $selector [, int $index] )
Find elements by the CSS selector. Returns the Nth element object if index is set, otherwise return an array of object.

4.find 方法详细介绍
find ( string $selector [, int $index] ) 
// Find all anchors, returns a array of element objects
$ret = $html->find( 'a' );

// Find (N)th anchor, returns element object or null if not found   (zero based)
$ret = $html->find( 'a', 0 );

// Find lastest anchor, returns element object or null if not found   (zero based)
$ret = $html->find( 'a', -1 ); 

// Find all <div> with the id attribute
$ret = $html->find( 'div[id]' );

// Find all <div> which attribute id=foo
$ret = $html->find( 'div[id=foo]' ); 
// Find all element which id=foo
$ret = $html->find( '#foo' );

// Find all element which class=foo
$ret = $html->find( '.foo' );

// Find all element has attribute id
$ret = $html->find( '*[id]' ); 

// Find all anchors and images 
$ret = $html->find( 'a, img' ); 

// Find all anchors and images with the "title" attribute
$ret = $html->find( 'a[title], img[title]' );
// Find all <li> in <ul> 
$es = $html->find( 'ul li' );

// Find Nested <div>  tags
$es = $html->find( 'div div div' ); 

// Find all <td> in <table> which class=hello 
$es = $html->find( 'table.hello td' );

// Find all td tags with attribite align=center in table tags 
$es = $html->find( ''table td[align=center]' );
5. Element  的方法
$e = $html->find( "div" 0 );                              // $e 所拥有的方法如下表所示
Attribute Name Usage
$e->tagRead or write the tag name of element.
$e->outertextRead or write the outer HTML text of element.
$e->innertextRead or write the inner HTML text of element.
$e->plaintextRead or write the plain text of element.
// Example
$ html =  str_get_html ( "<div>foo <b>bar</b></div>" ) ;  
$e = $html->find( "div" 0 );

echo $e-> tag // Returns: " div"
echo $e-> outertext // Returns: " <div>foo <b>bar</b></div>"
echo $e-> innertext // Returns: " foo <b>bar</b>"
echo $e-> plaintext // Returns: " foo bar"


6.DOM traversing 方法
Method Description
mixed
$e->children ( [int $index] )
Returns the Nth child object if index is set, otherwise return an array of children.
element
$e->parent ()
Returns the parent of element.
element
$e->first_child ()
Returns the first child of element, or null if not found.
element
$e->last_child ()
Returns the last child of element, or null if not found.
element
$e->next_sibling ()
Returns the next sibling of element, or null if not found.
element
$e->prev_sibling ()
Returns the previous sibling of element, or null if not found.

// Example
echo $html-> find ( "#div1", 0 )-> children ( 1 )-> children ( 1 )-> children ( 2 )-> id ;
// or  
echo $html-> getElementById ( "div1" )-> childNodes ( 1 )-> childNodes ( 1 )-> childNodes ( 2 )-> getAttribute ( 'id' );
    附带: DOM方法   set_callback ( 'my_callback' )  使用方法
// Write a function with parameter "$element"
function my_callback( $element ) {
         // Hide all <b> tags 
        if ($element->tag== 'b' )
                $element->outertext = '';


// Register the callback function with it's function name
$html-> set_callback ( 'my_callback' );

// Callback function will be invoked while dumping
echo $html;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

simple_html_dom使用小结 的相关文章

  • SVG 图标像素对齐?

    在此图像中 左侧 黑色 垃圾桶是图标字体 它的字体大小是 16px 这使得图标在 100 的时间里看起来都很清晰 两个蓝色垃圾桶是 SVG 它们具有完全相同的标记 其中之一happened一个是像素对齐的 另一个不是 我怎样才能强制我的 S
  • 引导下拉菜单 同一水平行上的两个链接

    我正在使用 Bootstrap 制作下拉菜单 我想要的选项之一是下拉菜单中同一水平行上的两个链接 我将如何实现这一目标 div class navbar div class navbar inner li class dropdown a
  • 如何获得浮动:右键垂直居中对齐

    我只是无法获得课程按钮align right垂直居中对齐 HTML div class panel footer span style width 100 class header footer item span div
  • CONDITION CSS 区分 IE6 到 IE7

    我想声明一个不同于 ie6 和 ie7 的风格 但我的CSS条件被IE7识别为IE6 我用的是XP和explorer 7 这是我使用的代码
  • 为什么 WebView 中的 dataWithPDFInsideRect 不能在 Mavericks 上创建高质量的 PDF?

    Run 示例项目 https github com tvarghese TestWebView并观察桌面上生成的输出 PDF 名为保存网页 pdf 粘贴感兴趣的代码片段 NSURL url NSBundle mainBundle URLFo
  • 使用外部按钮选择下一个/上一个单选按钮

    我正在制作一种幻灯片形式 当用户单击下一张图像时 还必须选择单选按钮 我的滑动功能可以正常工作 下一个按钮 也可以工作 但我有点坚持使用 上一个 按钮 不明白为什么它不起作用 fiddle http jsfiddle net V4tdx 这
  • Django - 渲染到字符串无法加载 CSS

    我正在尝试使用 Django 1 8 render to string 通过管理命令将 html 转换为 pdf 而不是使用 View request 以下代码可以将模板转换为 pdf 但它无法将 CSS 加载到模板中 def html t
  • 防止语言环境文件中的 HTML 字符实体被 Rails3 xss 保护破坏

    我们正在构建一个应用程序 这是我们第一个使用 Rails 3 的应用程序 并且我们必须从一开始就构建 I18n 作为完美主义者 我们希望在我们的视图中使用真正的排版 破折号 卷曲引号 省略号等 这意味着在我们的 locales xx yml
  • 了解客户端文件的对象 URL 以及如何释放内存

    我在用createObjectURL获取本地图像文件的引用 URL 当我完成文件 图像后 我打电话revokeObjectURL释放该内存 一切对我来说都很好 但我只是想确保我释放了我能释放的所有内存 我检查后出现了我的担忧chrome b
  • 如何使用javascript检查图像url是否为404

    使用案例 当 src 不为空并且 alt 标签不为空时 则显示 src 的图像 然后检查 src 图片 url 不是 404 当 src 为空且 alt 不为空时 显示名字的图像 当 src 和 alt 为空时显示默认图像 HTML img
  • 根据窗口大小调整 div 及其内部内容的大小

    我已经查找了一百万种技术 但我无法让它发挥作用 我知道还有其他类似的帖子 如果它给任何人带来麻烦 我很抱歉 但我需要针对我的代码的具体说明 因为我很愚蠢 提前非常感谢 我希望 div 容器 contentContactBox 以及其中的所有
  • 跨浏览器方法使子 div 适合其父级的宽度

    我正在寻找适合孩子的解决方案div进入它的父母width 我在这里看到的大多数解决方案不跨浏览器兼容 eg display table cell IE 不支持 lt 8 解决方案就是不声明width 100 默认为width auto 对于
  • 如何使用expressjs发送多个文件?

    我希望能够发送许多文件 如果可能的话 发送整个目录 以便我可以在从 html 文件调用的其他 js 文件中访问它 const app require express const http require http Server app co
  • 有条件地在 html.RadioButtonFor (MVC4/Razor) 中包含选中的属性

    当您在手动编码的 html 元素 例如单选按钮 中显式包含 checked 属性时 您可以使用 bool 来确定该属性是否存在于该元素上正如这里所看到的 http www davidhayden me blog conditional at
  • 在 iOS 上不显示数字键盘

    根据苹果的文档 http developer apple com library safari documentation AppleApplications Reference SafariHTMLRef Articles InputTy
  • 如何使

    我有一个列表 用作选项卡列表 div ul class TabControl li a href search Funds Funds 60 a li li a href search Companies Companies 4 a li
  • 如何使用 ui-router 中的 ui-sref 将参数传递给控制器

    我需要传递和接收两个参数到我想要转换到的状态ui srefui router 的 例如使用下面的链接将状态转换为home with foo and bar参数 a Go to home state with foo and bar para
  • 输入和文本区域可以拖动吗?

    MDN 规范以及我能通过 Google 找到的每个网站都说所有 HTML 元素都可以拖动 然而 在实践中 我发现我无法拖动文本输入或文本区域 即使它们已被禁用 例如 使用以下代码 img src http www placehold it
  • 为缺少字体的 Web 浏览器降低 Unicode 字符的质量

    我在 html 文档中使用 Unicode 检查标记 U 2713 我发现它在大多数浏览器中都可以正常显示 但偶尔我会遇到有人的电脑上缺少字体 如果字体丢失 是否有 HTML JS 技巧来指定替代显示字符 或图像 没有直接的方法可以判断任何
  • 如何防止 Safari 滚动溢出:隐藏的 iframe?

    使用 Safari 您可以通过设置 style overflow hide 来禁用大多数 iframe 滚动 在 iframe 上 但是 如果您单击 iframe 并移动鼠标 内容无论如何都会滚动 Example 滚动内容 html

随机推荐

  • 简记ReNet

    ResNet 论文给出了Resnet有3中类型 xff0c ResNet50 res3 res4 res6 res3 xff0c ResNet101 res3 res4 res23 res3 和ResNet152 res3 res4 res
  • triplet loss的原理及caffe代码

    1 xff1a triplet loss的原理及梯度推到 http blog csdn net tangwei2014 article details 46788025 2 xff1a triplet loss如何增加到caffe中 xff
  • object detection资料汇总

    转自 xff1a https handong1587 github io deep learning 2015 10 09 object detection html Jump to LeaderboardPapers R CNNMulti
  • 离职小记~~~

    这个月作出的最重要的决定 离职 对于已工作的人来说 xff0c 离职就是跳槽 xff0c 特别是对于我们计算机专业 xff0c 简简单单的一封辞职信 xff0c 等个半月 xff0c 办完手续 xff0c 一切OK xff0c 而且貌似越跳
  • C#控件编程详解之按钮控件

    摘要 xff1a 控件编程系列讲解之按钮控件 xff0c 以自身学习经历详解按钮控件的使用方法 编程语言 xff1a C 编程环境 xff1a Visual Studio 2019 目录 按钮控件属性 xff08 成员 xff09 详解 布
  • synchronized锁(方法锁, 代码块锁)

    synchronized 锁可以解决线程安全问题 xff0c 但是相应的 xff0c 只要是锁 xff0c 就会带来性能开销 xff0c 所以尽可能减小锁的范围尤为重要 synchronized锁无非修饰普通方法 xff0c 修饰静态方法
  • 骁龙855 plus的存在意义有限,高通5G芯片较对手落后

    近日高通突然发布了一款特别的芯片骁龙855 plus xff0c 让人可惜的是这并非是一款5G手机SOC芯片 xff0c 而仅仅是在骁龙855基础上提升性能的芯片 xff0c 在5G已商用之际推出的这款芯片非但没能提升高通的影响力 xff0
  • Spring之环境变量配置

    本地配置实现原理 项目启动时准备环境 public class SpringApplication public ConfigurableApplicationContext run String args long startTime 6
  • shell命令 在命令行中可行,但在sh文件中失效的解决办法

    大概率是因为文件编码格式问题 可以尝试对sh文件的编码格式进行修改 在vim中查看文件编码格式 xff1a set fileencoding 在vim中直接修改文件编码格式 xff1a set fileencoding 61 gbk 或者在
  • 黑苹果10.15.7使用

    咸鱼 由于懒得去折腾硬件 xff0c 所以直接在闲鱼上买了一个现成的 xff0c 已经安装了10 15 7 xff0c i3 8100 xff0c 256G的ssd xff0c 16G内存 两天从汕头到杭州 xff0c 不得不感叹当今快递业
  • 访问控制的几种模式

    一般分类 自主访问控制 xff1a Discretionary Access Control xff0c DAC 由客体的属主对自己的客体进行管理 xff0c 由属主自己决定是否将自己的客体访问权或部分访问权授予其他主体 xff0c 这种控
  • linux进程内存查看方法

    通过proc procid status查看进程内存 piky 64 piky WRT WX9 src test cat proc 3118 status Name a out Umask 0002 State S sleeping Tgi
  • 网络设备单点登录

    1 深信服 深信服的单点登录非常丰富 1 1 AD单点登录 1 1 1 通过域下发登录脚本的单点登录 通过AD服务器的组策略推送客户端的登录核退出程序 xff0c 两个exe程序 xff0c 实现相对容易 xff0c 但非windows客户
  • 基于openldap查询AD用户的组信息,上代码

    当用户找不到时 xff0c 这里的ldap result会卡住 xff0c 设置tm sec 为1也不行 需要继续定位 同样系统上官方的ldapsearch程序没有这个问题 span class token comment gcc ldap
  • linux 下记录ssh的操作日志

    ssh name 64 x x x x tee a log log tee可以读取标准输入输出的数据 xff0c 将其写到文件中
  • 云安全做什么?

    现在还说不清楚 xff0c 留下标题 xff0c 等能说清楚了后再补充
  • 用jpcap读取pcap抓包文件并做简单的过滤显示

    已经完成了最简单的从pcap文件中读取每个报文 xff0c 然后再转换成文本格式显示报文头信息 xff0c 以便于后面分析用 xff0c 这里做了简单的过滤 xff0c 根据源目的ip地址 xff0c 或者查找tcp没有响应的报文 以后又空
  • 【吐血整理】Java项目源码分享

    java servlet 43 jsp 43 bean开发开源宅商城系统 xff0c 未用任何java开源框架 实例 xff08 七 xff09 xff0c 电商书店 xff0c 源码下载 xff1a jsp 43 servlet仿当当网电
  • django操作数据库使用中文的方法

    今天在学习django时 xff0c 向mysql中写入数据 xff0c 结果提示错误 django db utils DatabaseError 1366 34 Incorrect string value 39 在网上找了很多资料 xf
  • simple_html_dom使用小结

    简单范例 lt php include 34 simple html dom php 34 Create DOM from URL or file html 61 file get html 39 http www google com 3