我需要從遠程頁面解析由javascript動態生成的內容。如何從本地JavaScript文件解析網頁
例如,我需要從該頁面的價格:http://www.alibaba.com/product-detail/BRG-Newest-Fashional-Protective-Case-For_1666645206.html#J-wrapper
而是由一個javascript產生的價格,所以當我下載使用Ajax的頁面,它只下載不結果的HTML文件腳本。
所以我試圖在後臺嵌入一個iframe,然後解析這個iframe中的文檔,但是存在一個安全問題,不能讓我解析它。
你知道我還能做到嗎?
我用的功能是這樣的:
$.ajax({
url: url,
dataType: 'text',
success: function(html, _, xhr) {});
但由此產生的HTML是沒有腳本的信息,這樣的價格是空的。
我試圖也用:
<html lang="en">
<head>
<meta charset="utf-8">
<title>contents demo</title>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<iframe src="http://api.jquery.com/" width="80%" height="600" id="frameDemo"></iframe>
<script>
document.getElementById("frameDemo").onload = function() {
var contents = $("#frameDemo").contents();
}
</script>
</body>
</html>
,但我得到這個錯誤:
Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFrameElement':
Blocked a frame with origin "null" from accessing a frame with origin "api.jquery.com".
The frame requesting access has a protocol of "file", the frame being accessed has a protocol of "http".
Protocols must match.
你的問題沒有得到 – 2014-09-19 12:48:44
它的工作.... – 2014-09-19 12:58:27
做這個服務器端以防止同域源策略 – 2014-09-19 12:59:45