首页 php教程 php手册 使用php simple html dom parser解析html标签

使用php simple html dom parser解析html标签

Jun 13, 2016 am 10:53 AM
dom html p parser php simple 使用 标签 解析

 

 

使用php simple html dom parser解析html标签

用了一下

PHP Simple HTML DOM Parser 

解析HTML页面,感觉还不错,它能创建一个DOM tree方便你解析html里面的内容。用来抓东西挺好的。

 

附带一个例子,你也到sourceforge下载压缩包看里面的例子:

Scraping data with PHP Simple HTML DOM Parser 

 

PHP Simple HTML DOM Parser , written in PHP5+, allows you to manipulate HTML in a very easy way. Supporting invalid HTML, this parser is better then other PHP scripts using complicated regexes to extract information from web pages.

Before getting the necessary info, a DOM should be created from either URL or file. The following script extracts links & images from a website:

view plain copy to clipboard print ? 

 

Php代码 // Create DOM from URL or file    

$html = file_get_html('http://www.microsoft.com/');    

   

// Extract links    

foreach($html->find('a') as $element)    

       echo $element->href . '
';     

   

// Extract images    

foreach($html->find('img') as $element)    

       echo $element->src . '
';  

[php] 

// Create DOM from URL or file   

$html = file_get_html('http://www.microsoft.com/');  

// Extract links   

foreach($html->find('a') as $element)  

       echo $element->href . '
';   

// Extract images   

foreach($html->find('img') as $element)  

       echo $element->src . '
';  

 

// Create DOM from URL or file

$html = file_get_html('http://www.microsoft.com/');

// Extract links

foreach($html->find('a') as $element)

       echo $element->href . '
'; 

// Extract images

foreach($html->find('img') as $element)

       echo $element->src . '
';

The parser can also be used to modify HTML elements:

view plain copy to clipboard print ? 

 

Php代码 // Create DOM from string    

$html = str_get_html('

Simple
Parser
');    

   

$html->find('div', 1)->class = 'bar';    

   

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

   

// Output:

Foo
Parser
   

echo $html;  

[php] 

// Create DOM from string   

$html = str_get_html('

Simple
Parser
');  

$html->find('div', 1)->class = 'bar';  

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

// Output:

Foo
Parser
  

echo $html;  

 

// Create DOM from string

$html = str_get_html('

Simple
Parser
');

$html->find('div', 1)->class = 'bar';

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

// Output:

Foo
Parser

echo $html;

Do you wish to retrieve content without any tags?

view plain copy to clipboard print ? 

 

Php代码 echo file_get_html('http://www.yahoo.com/')->plaintext;  

[php] 

echo file_get_html('http://www.yahoo.com/')->plaintext;  

 

echo file_get_html('http://www.yahoo.com/')->plaintext;In the package files of this parser ([url]http://simplehtmldom.sourceforge.net/[/url]) you can find some scraping examples from digg, imdb, slashdot. Let’s create one that extracts the first 10 results (titles only) for the keyword “php” from Google:

view plain copy to clipboard print ? 

 

Php代码 $url = 'http://www.google.com/search?hl=en&q=php&btnG=Search';    

   

// Create DOM from URL    

$html = file_get_html($url);    

   

// Match all 'A' tags that have the class attribute equal with 'l'    

foreach($html->find('a[class=l]') as $key => $info)    

{    

echo ($key + 1).'. '.$info->plaintext."
\n";    

}  

[php] 

$url = 'http://www.google.com/search?hl=en&q=php&btnG=Search';  

// Create DOM from URL   

$html = file_get_html($url);  

// Match all 'A' tags that have the class attribute equal with 'l'   

foreach($html->find('a[class=l]') as $key => $info)  

{  

echo ($key + 1).'. '.$info->plaintext."
\n";  

}  

 

$url = 'http://www.google.com/search?hl=en&q=php&btnG=Search';

// Create DOM from URL

$html = file_get_html($url);

// Match all 'A' tags that have the class attribute equal with 'l'

foreach($html->find('a[class=l]') as $key => $info)

{

echo ($key + 1).'. '.$info->plaintext."
\n";

}NOTE Make sure to include the parser before using any functions of it:

view plain copy to clipboard print ? 

Php代码 

include 'simple_html_dom.php';  

[php] 

include 'simple_html_dom.php';  

 

include 'simple_html_dom.php';For more information regarding the usage of this function consider checking the ‘PHP Simple HTML Dom Parser’ Manual. To download the package files use the following URL: [url]

分享到: 

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

CakePHP 项目配置 CakePHP 项目配置 Sep 10, 2024 pm 05:25 PM

在本章中,我们将了解CakePHP中的环境变量、常规配置、数据库配置和电子邮件配置。

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 Dec 24, 2024 pm 04:42 PM

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 日期和时间 CakePHP 日期和时间 Sep 10, 2024 pm 05:27 PM

为了在 cakephp4 中处理日期和时间,我们将使用可用的 FrozenTime 类。

CakePHP 使用数据库 CakePHP 使用数据库 Sep 10, 2024 pm 05:25 PM

在 CakePHP 中使用数据库非常容易。本章我们将了解CRUD(创建、读取、更新、删除)操作。

CakePHP 文件上传 CakePHP 文件上传 Sep 10, 2024 pm 05:27 PM

为了进行文件上传,我们将使用表单助手。这是文件上传的示例。

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

在本章中,我们将学习以下与路由相关的主题?

讨论 CakePHP 讨论 CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

CakePHP 创建验证器 CakePHP 创建验证器 Sep 10, 2024 pm 05:26 PM

可以通过在控制器中添加以下两行来创建验证器。

See all articles