PHPプログラムに関する各種メモ書き

PHPでWEBページをSQLライクに取得する。【htmlSQL】

■ htmlSQL

http://www.jonasjohn.de/lab/htmlsql.htm

使い方は

■ 例

http://codedump.jonasjohn.de/
というサイトの
クラスが "SnippetList" の ul タグを取得する場合
SELECT * FROM ul WHERE $class == "SnippetList"

というSQL文になります。

PHPコードで書くと以下の通り。

その他

htmlSQLのバリエーションとしては

■ ページのタイトルを取得する場合

SELECT text FROM title

<?php
    include_once("../snoopy.class.php");
    include_once("../htmlsql.class.php");
    $wsql = new htmlsql();
    // connect to a URL
    if (!$wsql->connect('url', 'http://codedump.jonasjohn.de/')){
        print 'Error while connecting: ' . $wsql->error;
        exit;
    }
    if (!$wsql->query('SELECT * FROM ul WHERE $class == "SnippetList"')){
        print "Query error: " . $wsql->error; 
        exit;
    }
    // show results:
    foreach($wsql->fetch_array() as $row){
        print_r($row);        
    }
?>

■ htmlSQLを使って取得したデータの日本語が文字化けする場合

htmlsql.class.php のソースの中の 201行目

$this->page = $this->snoopy->results;
$this->page = mb_convert_encoding( $this->page, 'UTF-8', 'AUTO' );	// これを追加

を付け足すことで回避できます。


参考:http://fdays.blogspot.com/2008/04/htmlsql.html


No.717
03/26 14:02

edit