0

Possible Duplicate:
How to parse and process HTML with PHP?

I want to retrieve the header and footer of a webpage (the owners know this) and display it on a new page so I can add in different content. The page is structured pretty nicely with the content inside a div with an id of content so I figured I could do the following:

Use CURL to retrieve the html Take the html either side of the content Echo it out onto a new page

My problem is I'm not too PHP savvy so I'm not sure how to take the two lumps of html either side. I've used substring in Java before but the substr in PHP seems to work a little differently. Can anyone suggest an alternative?

Thanks

4

3 に答える 3

2

Substring と RegEx は、HTML を処理するための十分なツールではありません。DOM パーサーを使用するのが最善 (そしてはるかに簡単) です。

DOMDocumentクラスを見てください。HTML の読み込みをサポートしており、ドキュメントを簡単にトラバースできます。

于 2012-10-22T16:47:40.433 に答える
1

Web ページをスクレイピングするために、HTML DOM パーサーを使用しました。これはあなたにとって最も簡単な方法です。この投稿でさらに多くのツールを見つけることができます: How to parse and process HTML with PHP?

于 2012-10-22T16:49:38.123 に答える
0

私は先日これと非常によく似たことをしました。jQuery、Ajax、および PHP を使用してページを収集し、それらを分解することにしました。コードの希釈バージョンを含めました。

PHP の場合、CURL (get-url.php) を使用しました。

$requestURL = $_GET['url'];
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $requestURL);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl_handle, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($curl_handle, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl_handle, CURLOPT_MAXREDIRS, 10);
curl_setopt($curl_handle, CURLOPT_DNS_USE_GLOBAL_CACHE, FALSE);
curl_setopt($curl_handle, CURLOPT_FORBID_REUSE, TRUE);
$content = curl_exec($curl_handle);
curl_close($curl_handle);
echo $content;

次に、Ajaxの場合、次を使用しました:

var url = /* URL you want to retrieve */;
$.ajax({
        url: "get-url.php?url=" + url,
        type: "GET",
        dataType: "html",
        cache: false,
        success: function(data, textStatus, jqXHR){
            var header = data.find('#header').html();
            var footer = data.find('#footer').html();
            $(header_DOM).html(header);
            $(footer_DOM).html(footer);
        }
    });

これは単なるガイドです。必要に応じて、この考え方を変更してください。

于 2012-10-22T17:02:29.620 に答える