簡體   English   中英

如何獲取網站源作為原始文本jQuery

[英]How To Get Website Source As Raw Text Jquery

我一直在到處搜索,但似乎找不到答案,我想用原始文本檢索網站的源代碼,我嘗試了一些類似的方法

    $("#divname").html('<object data="http://example.com/">');

但是,您得到的只是一個加載了網站的div,如果我將其更改為.text或.val,它將無法正常工作:/

任何幫助將不勝感激

通過使用file_get_contentsCurl

<?php
    echo '<div id="divname">' . htmlspecialchars(file_get_contents('http://example.com')) . '</div>';

您可以在php中使用curl(編寫簡單代理),然后調用ajax從url獲取頁面

file.php

if (isset($_POST['url'])) {
    $ua = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $_POST['url']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, $ua);
    echo curl_exec($ch);
}

並在javascript中使用ajax

$.ajax({
    url: 'file.php',
    type: 'POST',
    data: {url: 'http://example.com/'},
    dataType: 'text',
    success: function(html) {
       // process html
    }
});

我使用$.ajax函數,因為$.post會將內容解析為html而不是文本。

暫無
暫無

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

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