我有一个与此类似的问题,我使用 Nightmare 登录页面并浏览它。在此页面中,一些请求会获得 json 响应以填充数据(见“网络”选项卡)。有没有办法访问这个 json 而不是解析页面本身?
var Nightmare = require('nightmare');
const nightmare = Nightmare({ show: true });
nightmare
.goto('https://my.url/')
.type('#user_id', 'myUserId')
.type('#password', 'passw0rd')
.click('#button-login')
.wait(3000)
.goto('specific-url') // this URL loads a page with some data
.end()
.then(console.log) // this prints stuff like HTTP response: OK
.catch((error) => {
console.error('Search failed:', error);
});
我很感激任何帮助。谢谢
我无法自己测试这个,因为我没有使用 Nightmare 或 node 安装,但根据我在这里阅读的内容,这可能会成功:
var Nightmare = require('nightmare');
const nightmare = Nightmare({ show: true });
nightmare
.goto('https://my.url/')
.type('#user_id', 'myUserId')
.type('#password', 'passw0rd')
.click('#button-login')
.wait(3000)
.goto('specific-url') // this URL loads a page with some data
.wait(1000)
.evaluate(() => {
var jsonUrl = 'needs to contain the address of the JSON backend';
var filename = './json-result.json';
var file = fs.createWriteStream(filename);
var request = http.get(jsonUrl, function (response) {
response.pipe(file);
});
}
)
.end()
.then(console.log) // this prints stuff like HTTP response: OK
.catch((error) => {
console.error('Search failed:', error);
});
这要求您有一个正在调用的静态 URL,然后将返回 JSON 响应。如果你需要额外的参数传递,你可能会更好使用内部的XMLHttpRequest的evaluate()
像块描述这里。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句