簡體   English   中英

獲取html源代碼

[英]get html source code

我正在嘗試使用Javascript將任何網頁的html源代碼放入字符串中。 請告訴我是否可以做其他事情來解決我的問題。.我正在使用從另一篇文章中找到的以下代碼

function httpGet(theUrl)
{
var xmlHttp = null;

xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false );
xmlHttp.send( null );
return xmlHttp.responseText;
}

我在IE Firefox和Chrome中嘗試了此操作,但是我始終會獲得以下源代碼,這是“ PAGE NOT FOUND”頁面的源代碼。如果您有其他任何信息,請在評論中讓我知道。從任何網頁(例如google.com和其他網頁)獲取HTML。如果我不能這樣做,那我該怎么辦?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://gmpg.org/xfn/11">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>404 - PAGE NOT FOUND</title>
            <style type="text/css">
            body{padding:0;margin:0;font-family:helvetica;}
            #container{margin:20px auto;width:868px;}
            #container #top404{background-image:url('http://74.53.143.237/images/404top.gif');background-repeat:no-repeat;width:868px;height:168px;}
            #container #mid404{background-image:url('http://74.53.143.237/images/404mid.gif');background-repeat:repeat-y;width:868px;}
            #container #mid404 #gatorbottom{position:relative;left:39px;float:left;}
            #container #mid404 #xxx{float:left;padding:40px 237px 10px;}
            #container #mid404 #content{float:left;text-align:center;width:868px;}
            #container #mid404 #content #errorcode{font-size:30px;font-weight:800;}
            #container #mid404 #content p{font-weight:800;}
            #container #mid404 #content #banner{margin:20px 0 0 ;}
            #container #mid404 #content #hostedby{font-weight:800;font-size:25px;font-style:italic;margin:20px 0 0;}
            #container #mid404 #content #coupon{color:#AB0000;font-size:22px;font-style:italic;}
            #container #mid404 #content #getstarted a{color:#AB0000;font-size:31px;font-style:italic;font-weight:800;}
            #container #mid404 #content #getstarted {margin:0 0 35px;}
            #container #bottom404{background-image:url('http://74.53.143.237/images/404bottom.gif');background-repeat:no-repeat;width:868px;height:14px;}
            </style>
</head>
<body>
<div id="container">
    <div id="top404"></div>
    <div id="mid404">

            <div id="gatorbottom"><img src="http://74.53.143.237/images/gatorbottom.png" alt="" /></div>
            <div id="xxx"><img src="http://74.53.143.237/images/x.png" alt="" /></div>
    <div id="content">
            <div id="errorcode">ERROR 404 - PAGE NOT FOUND</div>
            <p>Oops! Looks like the page you're looking for was moved or never existed.<br />Make sure you typed the correct URL or followed a valid link.</p>

            <div id="banner">

                    <object width="728" height="90"><param name="movie" value="http://74.53.143.237/images/hg728x90.swf">

                            <embed src="http://74.53.143.237/images/hg728x90.swf?clickTAG=http://secure.hostgator.com/cgi-bin/affiliates/clickthru.cgi?id=page404" width="728" height="90"></embed>
                    </object>
            </div>

            <div id="hostedby">This site is hosted by HostGator!</div>
            <div id="coupon">Build your website today for 1 cent!   Coupon code: "404PAGE"</div>

            <div id="getstarted"><a href="http://www.hostgator.com/?utm_source=internal&utm_medium=link&utm_campaign=page404" title="HostGator Web Hosting" >CLICK HERE TO GET STARTED</a></div>

    </div>

    <div style="clear:left;"></div>
    </div>
    <div id="bottom404"></div>
</div>

</body>

</html>

我正在嘗試使用Javascript將任何網頁的html源代碼放入字符串中

如果用“任何”來表示來自文檔來源之外的其他來源的頁面,則不能通過運行在瀏覽器中的 JavaScript 來實現 ,因為您使用的是ajax調用,並且這些頁面受到Same Origin Policy的限制,它表示(例如)在http://stackoverflow.com上的文檔中運行的腳本不能使用Ajax從http://example.com加載內容。 (“來源”不只是域名,它涉及多個方面,有關詳細信息,請參見鏈接)。

有些你可能會請求的頁面(但可能很少 )可能支持跨來源資源共享 ,在這種情況下,如果他們讓你的原點(可能是允許所有起源),你可以使用Ajax加載的內容。

如果您正在瀏覽器外部運行JavaScript(NodeJS,SilkJS,RingoJS,Rhino,Windows腳本宿主等),那么SOP將不適用,但我懷疑您可能需要使用XMLHttpRequest對象以外的其他東西去做吧。

但從根本上講,在瀏覽器的網頁(不是擴展/附加組件)中,您不能這樣做。

...但是我總是得到“ PAGE NOT FOUND”頁面的源代碼

但是, 聽起來像URL是錯誤的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM