gpt4 book ai didi

javascript - 使用 Node.js,在 XMLHttpRequest 期间或之后创建一个新的 XMLHttpRequest

转载 作者:行者123 更新时间:2023-12-03 00:50:25 28 4
gpt4 key购买 nike

我对 Node.js 和 XMLHttpRequest 非常陌生,所以如果这是一个简单答案的问题,请耐心等待。

我目前正在尝试抓取一个 friend 的网页(当然要经过他的许可),其中包含视频和字幕。我想通过编写 Node.js 命令行应用程序来做到这一点。目前,我只是想获取视频的链接和字幕的链接。这是我到目前为止所拥有的:

#!/usr/bin/env node

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var htmlparser = require("htmlparser2");

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {

// HTML source
var html = this.responseText;
var season = 0;
var episode = 0;

var parser = new htmlparser.Parser({
onopentag: function(name, attribs) {
if (name === "li" && attribs.id === "season-1") {
season = 1;
console.log("In season 1");
for(var attr in attribs){
console.log(attr);
}
}
if (name === "a" && season === 1) {
episode = 1;
var nextPage = attribs.data;
console.log("\""+nextPage+"\"");

// Go to "nextPage" here
xhttp.open("GET", "\""+nextPage+"\"", true);

}
},
onattribute: function(name, value) {
if(name === "data-url" && season === 1){
if(value.includes("episode-")){
episode = value.substr(8,1);
}
console.log(value);
console.log("Episode is: " + episode)
}
},
ontext: function(text) {

},
onclosetag: function(tagname) {
if (tagname === "li" && season === 1) {
season = 0;
console.log("Leaving season 1");
}
}
}, {
decodeEntities: true
});
parser.write(html);
parser.end();

}
};
xhttp.open("GET", "https://friendspage.org", true);
xhttp.send();

上面的代码会产生以下输出:

In season 1
id
episode-1
Episode is: 1
"https://friendspage.org/episode-1"
episode-2
Episode is: 2
"https://friendspage.org/episode-2"
episode-3
Episode is: 3
"https://friendspage.org/episode-3"
episode-4
Episode is: 4
"https://friendspage.org/episode-4"
episode-5
Episode is: 5
"https://friendspage.org/episode-5"
episode-6
Episode is: 6
"https://friendspage.org/episode-6"
episode-7
Episode is: 7
"https://friendspage.org/episode-7"
episode-8
Episode is: 8
"https://friendspage.org/episode-8"
episode-9
Episode is: 9
"https://friendspage.org/episode-9"
Leaving season 1

代码按照我想要的方式工作,除了我想要转到nextPage的部分。我将使用命令行中的输入变量来选择要转到哪个页面,但目前我不知道如何转到 nextPage

// Go to "nextPage" here
xhttp.open("GET", "\""+nextPage+"\"", true);

尝试使用xhttp.send()会导致错误send has not been called。我猜我要么需要关闭当前请求并打开一个新请求,要么只是启动 XMLHttpRequest 的另一个实例。

如果这就是我需要做的,哪种是首选方法?最干净的方法是什么?如果没有,我需要使用什么解决方案?

最佳答案

为每个新请求在新变量中创建一个新的 xhttp 对象。不要重复使用当前的。

仅供引用,request-promise 库为 Node.js 构建的效果比尝试从浏览器模拟古老的界面要好得多(甚至它已被 fetch() 取代))。 Node.js 中内置的用于执行此操作的功能是 http.get(),但该功能已在 request 库中以非常有用的方式进行了扩展,然后已被 promise 在 request-promise 库中,这使得它更容易使用。我强烈建议使用 request-promise 库来完成此任务。

<小时/>

编辑 2020 年 1 月 - request() 模块处于维护模式

仅供引用,request 模块及其衍生产品,如 request-promise现在处于维护模式,不会积极开发以添加新功能。更多推理可以阅读herethis table 中有一个替代方案列表对每一项进行一些讨论。我一直用got()我自己,它从一开始就使用 Promise 构建,并且使用起来很简单。

关于javascript - 使用 Node.js,在 XMLHttpRequest 期间或之后创建一个新的 XMLHttpRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53072832/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com