ホームページ 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 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

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:27 PM

ファイルのアップロードを行うには、フォーム ヘルパーを使用します。ここではファイルアップロードの例を示します。

CakePHP ルーティング CakePHP ルーティング Sep 10, 2024 pm 05:25 PM

この章では、ルーティングに関連する次のトピックを学習します。

CakePHP について話し合う CakePHP について話し合う Sep 10, 2024 pm 05:28 PM

CakePHP は、PHP 用のオープンソース フレームワークです。これは、アプリケーションの開発、展開、保守をより簡単にすることを目的としています。 CakePHP は、強力かつ理解しやすい MVC のようなアーキテクチャに基づいています。モデル、ビュー、コントローラー

HTML テーブルのレイアウト HTML テーブルのレイアウト Sep 04, 2024 pm 04:54 PM

HTML テーブル レイアウトのガイド。ここでは、HTML テーブル レイアウトの値と例および出力について詳しく説明します。

PHP 開発用に Visual Studio Code (VS Code) をセットアップする方法 PHP 開発用に Visual Studio Code (VS Code) をセットアップする方法 Dec 20, 2024 am 11:31 AM

Visual Studio Code (VS Code とも呼ばれる) は、すべての主要なオペレーティング システムで利用できる無料のソース コード エディター (統合開発環境 (IDE)) です。 多くのプログラミング言語の拡張機能の大規模なコレクションを備えた VS Code は、

See all articles