如何从本地javascript文件解析网页

How to parse a web page from a local javascript file

本文关键字:文件 网页 javascript      更新时间:2023-09-26

我需要解析javascript从远程页面动态生成的内容。

例如,我需要从这个页面获取价格:http://www.alibaba.com/product-detail/BRG-Newest-Fashional-Protective-Case-For_1666645206.html#J-包装

但是价格是由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.

不要使用iframe,而是尝试使用chrome应用程序webview。它将在没有您引用的限制的隔离容器中运行。但你仍然可以与它通信。