我需要将一个外部网页(不是本地)页面加载到我的网站(一些链接)中,但只是其中的一部分。这样做有哪些选择?
4 回答
这取决于外部页面是本地的还是位于不同的域中。如果它是本地的,您可以在 jQuery 库中使用 $.load()。这有一个可选参数来指定远程 dom 中的哪个元素来加载它:
$("#links").load("/Main_Page #jq-p-Getting-Started li");
如果页面在另一个域上,您将需要一个代理脚本。您可以使用 PHP 和phpQuery(jQuery 的 php 端口)库来做到这一点。您只需使用 file_get_contents() 来获取实际的 remote-dom,然后根据类似 jQuery 的选择器提取所需的元素。
Once you get the whole page as Michael Todd outlined, you will likely need to either use substring methods for a static means to slice up the content or you can use regex's for a more dynamic way to grab the content. An intro article on Regex's in ASP.Net can be found here. Good luck!
要在 .Net 中加载网页,请使用 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();
}
请注意,这将返回整个页面,而不仅仅是其中的一部分。然后,您需要筛选页面以找到您要查找的信息。
$f = fopen('http://www.quran.az/2/255', 'r');
等等...