简体   繁体   English

节点js-尝试发送POST请求,但未加载javascript内容

[英]Node js - Trying to send POST request, but it is not loading javascript content

I have been trying to send a POST request to a website, and it has not been loading the full site. 我一直在尝试向网站发送POST请求,但尚未加载完整的网站。

I am guessing that this is a problem with node js not loading the javascript content, then executing it. 我猜这是节点js不加载javascript内容然后执行它的问题。 I can SEE the javascript in the response, though it never loads the content that the javascript files are producing in the html. 我可以看到在响应的JavaScript,但它永远不会将加载的JavaScript文件在HTML生成的内容。

What I mean is, it doesn't actually execute the javascript. 我的意思是,它实际上并未执行javascript。 How would I make node js code to overcome this, and wait for the the full website to load? 我将如何制作节点js代码来克服这一问题,并等待完整的网站加载? I have tried native code, and the "request' module, both producing the same errors. If it helps, I don't need native javascript. Any modules would be fine. 我已经尝试了本机代码和“请求”模块,两者均产生相同的错误。如果有帮助,则不需要本机javascript。任何模块都可以。

Some code I have tried (request module): 我尝试过的一些代码(请求模块):

var options = {
method: 'POST',
uri: 'https://url',
formData: {
    'email': 'Email',
    'prize': '0',
    'transactional': 'on'
},
headers: {
    /* 'content-type': 'application/x-www-form-urlencoded' */ // Is set automatically
}
};

rp(options)
.then(function (body) {
   console.log(body)
})
.catch(function (err) {
   console.log(err)
});

When you fetch the contents of a URL or POST and get a response, that response is just some text. 当您获取URL或POST的内容并获得响应时,该响应只是一些文本。 node.js does not parse the content. node.js不会解析内容。 It does not execute Javascript in that content. 它不会在该内容中执行Javascript。 That's what a browser does - node.js is not a browser. 那就是浏览器的作用-node.js不是浏览器。 So, what you get is the RAW content that would be downloaded to a browser form that website. 因此,您得到的是可以从该网站下载到浏览器的RAW内容。 If it's an HTML response, then you just get the raw HTML page and no embedded scripts are fetched or run. 如果是HTML响应,那么您仅获得原始HTML页面,并且不会获取或运行任何嵌入式脚本。

If you want that page parsed and scripts run in it, then you need to get a component you can use inside of node.js that mimics what a browser does. 如果要解析该页面并在其中运行脚本,则需要获取可在node.js内部使用的组件,该组件可以模仿浏览器的功能。 It's not too hard to get an HTML parser, but it's a lot more work to actually fetch and run the Javascript in that page and do that safely in a way that does not put your server at risk. 获得HTML解析器并不难,但是要实际获取并运行该页面中的Javascript并以不会使服务器面临风险的方式安全地进行操作,要付出更多的努力。 In fact, Google generally doesn't even run your Javascript when indexing web pages (though they have been working on that technology for some circumstances). 实际上,在为网页建立索引时,Google通常甚至不会运行您的Java脚本(尽管在某些情况下它们一直在使用该技术)。

jsDOM and cheerio are a couple modules you can use in node.js for parsing the HTML from a response, jsDOM will execute scripts in the page, cheerio will not. jsDOMcheerio是可以在node.js中用于从响应中解析HTML的几个模块,jsDOM将在页面中执行脚本,而cheerio则不会。

Another package you could use is phantomJS which can serve as a webkit-based, headless browser which you can communicate with from node.js. 您可以使用的另一个软件包是phantomJS ,它可以用作基于Webkit的无头浏览器,您可以从node.js与之通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM