如何从网页获得源代码?

时间:2021-02-09 07:22:22

How can we get the source code of a webpage from a webpage in php and/or javascript?

如何从php和/或javascript的网页获取网页的源代码?

2 个解决方案

#1


3  

Thanks to:

感谢:

First, you must know that you will never be able to get the source code of a page that is not on the same domain as your page in javascript. (See http://en.wikipedia.org/wiki/Same_origin_policy).

首先,您必须知道,您将永远无法获得与javascript页面相同的页面的源代码。(见http://en.wikipedia.org/wiki/Same_origin_policy)。

In PHP, this is how you do it :

file_get_contents($theUrl);

In javascript, there is three ways :

Firstly, by XMLHttpRequest : http://jsfiddle.net/635YY/1/

首先,通过XMLHttpRequest: http://jsfiddle.net/635YY/1/。

var url="../635YY",xmlhttp;//Remember, same domain
if("XMLHttpRequest" in window)xmlhttp=new XMLHttpRequest();
if("ActiveXObject" in window)xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
xmlhttp.open('GET',url,true);
xmlhttp.onreadystatechange=function()
{
    if(xmlhttp.readyState==4)alert(xmlhttp.responseText);
};
xmlhttp.send(null);

Secondly, by iFrames : http://jsfiddle.net/XYjuX/1/

其次,通过iframe: http://jsfiddle.net/XYjuX/1/。

var url="../XYjuX";//Remember, same domain
var iframe=document.createElement("iframe");
iframe.onload=function()
{
    alert(iframe.contentWindow.document.body.innerHTML);
}
iframe.src=url;
iframe.style.display="none";
document.body.appendChild(iframe);

Thirdly, by jQuery : http://jsfiddle.net/edggD/2/

第三,jQuery: http://jsfiddle.net/edggD/2/。

$.get('../edggD',function(data)//Remember, same domain
{
    alert(data);
});

#2


1  

Ajax example using jQuery:

使用jQuery Ajax例子:

// Display the source code of a web page in a pre tag (escaping the HTML).
// Only works if the page is on the same domain.

$.get('page.html', function(data) {
    $('pre').text(data);
});

If you just want access to the source code, the data parameter in the above code contains the raw HTML source code.

如果您只想访问源代码,上面代码中的数据参数包含原始的HTML源代码。

#1


3  

Thanks to:

感谢:

First, you must know that you will never be able to get the source code of a page that is not on the same domain as your page in javascript. (See http://en.wikipedia.org/wiki/Same_origin_policy).

首先,您必须知道,您将永远无法获得与javascript页面相同的页面的源代码。(见http://en.wikipedia.org/wiki/Same_origin_policy)。

In PHP, this is how you do it :

file_get_contents($theUrl);

In javascript, there is three ways :

Firstly, by XMLHttpRequest : http://jsfiddle.net/635YY/1/

首先,通过XMLHttpRequest: http://jsfiddle.net/635YY/1/。

var url="../635YY",xmlhttp;//Remember, same domain
if("XMLHttpRequest" in window)xmlhttp=new XMLHttpRequest();
if("ActiveXObject" in window)xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
xmlhttp.open('GET',url,true);
xmlhttp.onreadystatechange=function()
{
    if(xmlhttp.readyState==4)alert(xmlhttp.responseText);
};
xmlhttp.send(null);

Secondly, by iFrames : http://jsfiddle.net/XYjuX/1/

其次,通过iframe: http://jsfiddle.net/XYjuX/1/。

var url="../XYjuX";//Remember, same domain
var iframe=document.createElement("iframe");
iframe.onload=function()
{
    alert(iframe.contentWindow.document.body.innerHTML);
}
iframe.src=url;
iframe.style.display="none";
document.body.appendChild(iframe);

Thirdly, by jQuery : http://jsfiddle.net/edggD/2/

第三,jQuery: http://jsfiddle.net/edggD/2/。

$.get('../edggD',function(data)//Remember, same domain
{
    alert(data);
});

#2


1  

Ajax example using jQuery:

使用jQuery Ajax例子:

// Display the source code of a web page in a pre tag (escaping the HTML).
// Only works if the page is on the same domain.

$.get('page.html', function(data) {
    $('pre').text(data);
});

If you just want access to the source code, the data parameter in the above code contains the raw HTML source code.

如果您只想访问源代码,上面代码中的数据参数包含原始的HTML源代码。