我一直在到处搜索,但似乎找不到答案,我想以原始文本检索网站的源代码,我尝试了一些类似的方法
$("#divname").html('<object data="http://example.com/">');
但是你得到的只是一个加载了网站的 div,如果我将它更改为 .text 或 .val 它将无法工作:/
任何帮助将不胜感激
我一直在到处搜索,但似乎找不到答案,我想以原始文本检索网站的源代码,我尝试了一些类似的方法
$("#divname").html('<object data="http://example.com/">');
但是你得到的只是一个加载了网站的 div,如果我将它更改为 .text 或 .val 它将无法工作:/
任何帮助将不胜感激
您可以在 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 而不是文本。
通过使用file_get_contents或Curl。
<?php
echo '<div id="divname">' . htmlspecialchars(file_get_contents('http://example.com')) . '</div>';