加载Html页面并修改部分

时间:2009-08-31 20:25:34

标签: asp.net html embedded-resource

我需要将外部网页(非本地)页面加载到我的网站(某些链接),但只是其中的一部分。 这样做有哪些选择?

4 个答案:

答案 0 :(得分:1)

这取决于外部页面是本地页面还是其他域名。如果它是本地的,你可以在jQuery库中使用$ .load()。这有一个可选参数来指定remote-dom中要加载它的元素:

$("#links").load("/Main_Page #jq-p-Getting-Started li");

如果页面位于其他域上,则需要代理脚本。您可以使用PHP和phpQuery(jQuery的php端口)库来完成此操作。你只需使用file_get_contents()来获取实际的remote-dom,然后根据类似jQuery的选择器拉出你想要的元素。

答案 1 :(得分:0)

$f = fopen('http://www.quran.az/2/255', 'r');

依旧......

答案 2 :(得分:0)

要在.Net中加载网页,请使用HttpWebRequest类。

取自MSDN的示例,here

    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();
}

请注意,这将返回整个页面,而不仅仅是其中的一部分。然后,您需要筛选页面以查找您正在查找的信息。

答案 3 :(得分:0)

一旦你按照迈克尔托德所描述的那样获得整个页面,你可能需要使用子串方法来获取静态方法来切片内容,或者你可以使用正则表达式以更动态的方式来获取内容。可以找到关于ASP.Net中正则表达式的介绍文章here。祝你好运!