2009-08-31 84 views

回答

1

这取决于外部页面是本地还是不同的域。如果它是本地的,你可以在jQuery库中使用$ .load()。这有一个可选的参数来指定哪些元素在远程DOM加载它:

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

如果页面上的其他领域,你需要一个代理脚本。你可以用PHP和phpQuery(php的jQuery端口)库来做到这一点。您只需使用file_get_contents()来获取实际的remote-dom,然后根据类似jQuery的选择器提取所需的元素。

+0

页面不是本地的,asp.net中是否有任何图书馆? – Wineshtain 2009-08-31 20:32:43

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

等等...

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

注意这将返回整个页面而不是它只是一个部分。然后,您需要筛选页面以查找您要查找的信息。

0

一旦按照Michael Todd的说法得到整个页面,您可能需要使用子字符串方法来静态分割内容,或者您​​可以使用regex以更动态的方式来获取内容。关于正则表达式在ASP.Net的介绍文章可以找到here。祝你好运!

+0

OP也可以尝试使用'XmlDocument'来解析页面并获取特定的节点。 – outis 2009-09-01 20:00:57