[英]How To Get Website Source As Raw Text Jquery
我一直在到處搜索,但似乎找不到答案,我想用原始文本檢索網站的源代碼,我嘗試了一些類似的方法
$("#divname").html('<object data="http://example.com/">');
但是,您得到的只是一個加載了網站的div,如果我將其更改為.text或.val,它將無法正常工作:/
任何幫助將不勝感激
通過使用file_get_contents或Curl 。
<?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.