1

我一直在到处搜索,但似乎找不到答案,我想以原始文本检索网站的源代码,我尝试了一些类似的方法

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

但是你得到的只是一个加载了网站的 div,如果我将它更改为 .text 或 .val 它将无法工作:/

任何帮助将不胜感激

4

2 回答 2

1

您可以在 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 而不是文本。

于 2013-06-24T12:40:59.347 回答
1

通过使用file_get_contentsCurl

<?php
    echo '<div id="divname">' . htmlspecialchars(file_get_contents('http://example.com')) . '</div>';
于 2013-06-24T12:33:29.057 回答