外部 Web (ローカルではない) ページを自分のサイト (いくつかのリンク) にロードする必要がありますが、その一部だけです。そうするためのオプションは何ですか?
4 に答える
これは、外部ページがローカルであるか、別のドメインであるかによって異なります。ローカルの場合は、jQuery ライブラリで $.load() を使用できます。これには、remote-dom 内のどの要素をロードするかを指定するオプションのパラメーターがあります。
$("#links").load("/Main_Page #jq-p-Getting-Started li");
ページが別のドメインにある場合は、プロキシ スクリプトが必要になります。これは、PHP とphpQuery (jQuery の php ポート) ライブラリを使用して行うことができます。file_get_contents() を使用して実際のリモート DOM を取得し、jQuery のようなセレクターに基づいて必要な要素を引き出します。
$f = fopen('http://www.quran.az/2/255', 'r');
等々...
.Net で Web ページを読み込むには、HttpWebRequest クラスを使用します。
ここでMSDNから取られた例:
    private string StringGetWebPage(String uri)
    {
        const int bufSizeMax = 65536; // max read buffer size conserves memory
        const int bufSizeMin = 8192;  // min size prevents numerous small reads
        StringBuilder sb;
        // A WebException is thrown if HTTP request fails
        try 
        {
            // Create an HttpWebRequest using WebRequest.Create (see .NET docs)!
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            // Execute the request and obtain the response stream
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            // Content-Length header is not trustable, but makes a good hint.
            // Responses longer than int size will throw an exception here!
            int length = (int)response.ContentLength;
            // Use Content-Length if between bufSizeMax and bufSizeMin
            int bufSize = bufSizeMin;
            if (length > bufSize)
                bufSize = length > bufSizeMax ? bufSizeMax : length;
            // Allocate buffer and StringBuilder for reading response
            byte[] buf = new byte[bufSize];
            sb = new StringBuilder(bufSize);
            // Read response stream until end
            while ((length = responseStream.Read(buf, 0, buf.Length)) != 0)
                sb.Append(Encoding.UTF8.GetString(buf, 0, length));
        }
        catch (Exception ex)
        {
            sb = new StringBuilder(ex.Message);
        }
        return sb.ToString();
}
これにより、ページの一部だけでなくページ全体が返されることに注意してください。次に、探している情報を見つけるためにページをふるいにかける必要があります。
Michael Todd が概説したようにページ全体を取得したら、コンテンツをスライスする静的な手段として部分文字列メソッドを使用するか、より動的な方法でコンテンツを取得するために正規表現を使用する必要があります。ASP.Net の Regex に関する紹介記事は、ここにあります。幸運を!