14
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

phpでhtmlをスクレイピングしてみる

Last updated at Posted at 2014-11-09

簡単にライブラリを作ってみたので使ってみたいと思います。
javascriptの感覚で...

まだまだ未完成な部分もありますが、大抵パース可能と思います。

https://github.com/masahiroike/chdocument
http://masahiroike.com/blog/php-chdocument-html-parser/

PHP

//classをロード
include chdocument.php;

//コンストラクターを作成
//第二引数は任意で文字コードの指定が出来ます。
//デフォルトでは自動判定になっています。
//stringを入力する事も可能です。その際はURLに文字列を入れてください。
$document = chdocument(URL);

//idでnodeを取得する
$ID = $document -> getElementById(ID);

//classでnodeを取得する
$CLASS0 = $document -> getElementsByClassName(CLASS) -> item(0);

//nameでnodeを取得する
$NAME0 = $document -> getElementsByName(NAME) -> item(0);

//tagでnodeを取得する
$TAG0 = $document -> getElementsByTagName(TAG) -> item(0);

//itemの数をカウントする
数 = $TAG0 -> length;

//nodeのタグ名を取得する
タグ名 = $TAG0 -> tagName;

//属性の値を取得する
値 = $TAG0 -> 属性名;

//ページ内すべてのアンカーを取得する
$content = '';
$a = $document -> getElementsByTagName('a');
for($i = 0, $count = $a -> length; $i < $count; $i++) {
 $content .= $a -> item($i) -> outerHTML;
}

//ページ内すべてのアンカーのhrefを取得する
for($i = 0, $count = $a -> length; $i < $count; $i++) {
 $content .= $a -> item($i) -> href;
}

//ページ内すべてのアンカーのinnerTextを取得する
for($i = 0, $count = $a -> length; $i < $count; $i++) {
 $content .= $a -> item($i) -> innerHTML;
}

//指定したnodeの親nodeを取得する
$li = $document -> getElementsByTagName('li');
$ul = $li -> item(0) -> parentNode;

//指定したnodeの子nodeを取得する
//※戻り値が配列なので、注意してください。
$child = $ul -> children;
$child = $ul -> children[0] -> outerHTML;

//nodeに属性を付与する
$option = $document -> getElementsByTagName('option');
$option -> item(0) -> selected = true;
$option -> item(0) -> value = 'testString';

//nodeのinnerを書き換える
$document -> getElementsByTagName('a') -> item(0) -> innerHTML = 'リンクテキスト';

//nodeのouterを書き換える
$document -> getElementsByTagName('a') -> item(0) -> outerHTML = '<p>もともとリンクです</p>';

//取得変更したページを表示
echo $document;

//classをロード
include chdocument.php;
 
//コンストラクターを作成
//第二引数は任意で文字コードの指定が出来ます。
//デフォルトでは自動判定になっています。
//stringを入力する事も可能です。その際はURLに文字列を入れてください。
$document = chdocument(URL);
 
//idでnodeを取得する
$ID = $document -> getElementById(ID);
 
//classでnodeを取得する
$CLASS0 = $document -> getElementsByClassName(CLASS) -> item(0);
 
//nameでnodeを取得する
$NAME0 = $document -> getElementsByName(NAME) -> item(0);
 
//tagでnodeを取得する
$TAG0 = $document -> getElementsByTagName(TAG) -> item(0);
 
//itemの数をカウントする
数 = $TAG0 -> length;
 
//nodeのタグ名を取得する
タグ名 = $TAG0 -> tagName;
 
//属性の値を取得する
値 = $TAG0 -> 属性名;
 
//ページ内すべてのアンカーを取得する
$content = '';
$a = $document -> getElementsByTagName('a');
for($i = 0, $count = $a -> length; $i < $count; $i++) {
 $content .= $a -> item($i) -> outerHTML;
}
 
//ページ内すべてのアンカーのhrefを取得する
for($i = 0, $count = $a -> length; $i < $count; $i++) {
 $content .= $a -> item($i) -> href;
}
 
//ページ内すべてのアンカーのinnerTextを取得する
for($i = 0, $count = $a -> length; $i < $count; $i++) {
 $content .= $a -> item($i) -> innerHTML;
}
 
//指定したnodeの親nodeを取得する
$li = $document -> getElementsByTagName('li');
$ul = $li -> item(0) -> parentNode;
 
//指定したnodeの子nodeを取得する
//※戻り値が配列なので、注意してください。
$child = $ul -> children;
$child = $ul -> children[0] -> outerHTML;
 
//nodeに属性を付与する
$option = $document -> getElementsByTagName('option');
$option -> item(0) -> selected = true;
$option -> item(0) -> value = 'testString';
 
//nodeのinnerを書き換える
$document -> getElementsByTagName('a') -> item(0) -> innerHTML = 'リンクテキスト';
 
//nodeのouterを書き換える
$document -> getElementsByTagName('a') -> item(0) -> outerHTML = '<p>もともとリンクです</p>';
 
//取得変更したページを表示
echo $document;

14
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?