Maison 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]

分享到: 

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Article chaud

Musée de deux points: Guide de localisation de Bungle Wasteland
4 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD
Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌

Article chaud

Musée de deux points: Guide de localisation de Bungle Wasteland
4 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD
Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌

Tags d'article chaud

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Date et heure de CakePHP Date et heure de CakePHP Sep 10, 2024 pm 05:27 PM

Date et heure de CakePHP

Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian Dec 24, 2024 pm 04:42 PM

Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian

Téléchargement de fichiers CakePHP Téléchargement de fichiers CakePHP Sep 10, 2024 pm 05:27 PM

Téléchargement de fichiers CakePHP

Disposition du tableau HTML Disposition du tableau HTML Sep 04, 2024 pm 04:54 PM

Disposition du tableau HTML

Routage CakePHP Routage CakePHP Sep 10, 2024 pm 05:25 PM

Routage CakePHP

Discuter de CakePHP Discuter de CakePHP Sep 10, 2024 pm 05:28 PM

Discuter de CakePHP

Guide rapide CakePHP Guide rapide CakePHP Sep 10, 2024 pm 05:27 PM

Guide rapide CakePHP

Configuration du projet CakePHP Configuration du projet CakePHP Sep 10, 2024 pm 05:25 PM

Configuration du projet CakePHP

See all articles