2014-09-19 55 views
0

我需要從遠程頁面解析由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. 
+0

你的問題沒有得到 – 2014-09-19 12:48:44

+0

它的工作.... – 2014-09-19 12:58:27

+0

做這個服務器端以防止同域源策略 – 2014-09-19 12:59:45

回答

0

而不是使用iframe的,請嘗試使用Chrome應用的WebView。它將在沒有您引用的限制的獨立容器中運行。但你仍然可以與它溝通。

+0

謝謝,我不知道,我會立即嘗試,並告訴你它是否有效,非常感謝! – Alberto 2014-09-19 13:16:43

+0

看起來,webview僅適用於Chrome應用程序,而不適用於單一html文件。我將探討這種可能性,但如果可以完成的話,我仍然在尋找一種方法來完成它。 – Alberto 2014-09-19 13:43:23

+0

僅供參考,這裏是webview的鏈接:https://developer.chrome.com/apps/tags/webview – Alberto 2014-09-19 13:52:40