获取parse.com CloudCode的html源代码w / XMLHttpRequest.js
对于云代码(parse.com),我试图从另一个网站上刮取网页数据,但我一直无法获得网站的源代码作为一个string。
我尝试使用xmlhttprequest模块
var url = "www.targetWebsite.com"; var XMLHttpRequest = require("cloud/XMLHttpRequest.js").XMLHttpRequest; var xmlHttp = new XMLHttpRequest(); xmlHttp.open( "GET", url, false ); xmlHttp.send( null ); var doc = xmlHttp.responseText;
但是,当我尝试运行代码,我得到错误Module child_process.js not found
我假设在XMLHttpRequest.js文件中引用此行
var spawn = require("child_process").spawn
但是,我找不到下载文件夹中的child_process.js
添加到目录。
有没有办法包含这个文件,还是有更好的方法来获取源代码?
编辑:使用httpRequest云function
Parse.Cloud.define("pushFavorites", function(request, response) { var xpath = require("cloud/xpath.js"), dom = require("cloud/dom-parser.js").DOMParser; var doc; Parse.Cloud.httpRequest({ url: "website.com", success: function(httpResponse) { doc = new dom().parseFromString(httpResponse.text); }, error: function(httpResponse) { console.error('Request failed with response code ' + httpResponse.status); } }); var cells = xpath.select("//td[starts-with(@class, 'menugridcell')]", doc); //etc...
在声明cells
variables的行上,我收到错误: Cannot read property 'nodeType' of undefined
使用console.log, httpResponse.text
将源代码正确显示为一个string。 我不知道如果错误是与httpResponse或我的xpath。 我能够得到xpath.select()函数正常工作的一些其他人工devise的XMLstring。
parsing云代码不运行节点,所以虽然你可以得到一些模块的工作,并不是所有的意志。 在这种情况下,我怀疑你可以作为child_process是一个核心节点模块(请参阅Where is child_process.js? ),因此在Cloud Code中不可用。
尝试使用Parse.Cloud.httpRequest,而应该能够满足您的需求。