Javascript 在一个窗口中循环不同的URL
有一组URL。(URL1、URL2、URL3等)。 我想打开一个新窗口,在新窗口中打开这些URL,从每个URL中逐个抓取数据。 我正在使用newWindows.location.replace(URL[I]);更改窗口的href。 我的问题是我找不到一种方法来等待每个URL完全加载并收集数据 p、 美国。 我不是一个专业的程序员。我只是想用Violentmonkey从网页内的表中获取数据。 表格如下:Javascript 在一个窗口中循环不同的URL,javascript,loops,window,Javascript,Loops,Window,有一组URL。(URL1、URL2、URL3等)。 我想打开一个新窗口,在新窗口中打开这些URL,从每个URL中逐个抓取数据。 我正在使用newWindows.location.replace(URL[I]);更改窗口的href。 我的问题是我找不到一种方法来等待每个URL完全加载并收集数据 p、 美国。 我不是一个专业的程序员。我只是想用Violentmonkey从网页内的表中获取数据。 表格如下: <tableclass="fltlist_table"> ... <tbo
<tableclass="fltlist_table">
...
<tbodyid="flt1">
<tr>
<td>...</td>
</tr>
</tbody>
</table>
好吧,我终于解决了这个问题。我的代码如下:
//original nextPage.href is http, main page is https.
newURL = nextPage.href.replace(/http/, "https");
//New window
newWindow = window.open(newURL, "FilghtWindow", "directories=no,resizable=no, width=400, height=400");
newWindow.focus();
var winLoaded = function(){
//Collect rest
finalData = GetData(newWindow.document);
//Next
nextPage = newWindow.document.getElementsByClassName("schedule_down")[0];
if (nextPage){
newURL = nextPage.href.replace(/http/, "https");
newWindow.location.replace(newURL);
}else{
//Finish
console.log(finalData);
newWindow.close();
}
}
var winUnloaded = function(){
setTimeout(function(){
newWindow.onload= winLoaded;
newWindow.onunload = winUnloaded;
},0);
}
//add handle
newWindow.onload= winLoaded;
newWindow.onunload = winUnloaded;
}
enter code here
正如所建议的那样,您最好是发出HTTP请求,而不是打开新窗口。这可以通过使用
希望这有帮助。祝你好运 听起来您想发出get请求,而不是重定向到页面。@是的,我想发出一组请求。但是,在从URL获取数据时,一个新窗口每次显示一个不同的URL会更好。@TaimoorSuleman我搜索了很长时间,但没有得到任何答案。可能是因为我找不到完美的关键词来搜索。而且,我试过几次发布我的代码。但它总是说代码的格式不正确,或者要求我添加一些上下文。我不知道如何发布代码。嗨,vmorsell。非常感谢。你的代码结构几乎是我需要的。我只是想从网页内的表格中获取数据。我不知道怎么用fetch来做。请在我的问题中查看我的代码。
//original nextPage.href is http, main page is https.
newURL = nextPage.href.replace(/http/, "https");
//New window
newWindow = window.open(newURL, "FilghtWindow", "directories=no,resizable=no, width=400, height=400");
newWindow.focus();
var winLoaded = function(){
//Collect rest
finalData = GetData(newWindow.document);
//Next
nextPage = newWindow.document.getElementsByClassName("schedule_down")[0];
if (nextPage){
newURL = nextPage.href.replace(/http/, "https");
newWindow.location.replace(newURL);
}else{
//Finish
console.log(finalData);
newWindow.close();
}
}
var winUnloaded = function(){
setTimeout(function(){
newWindow.onload= winLoaded;
newWindow.onunload = winUnloaded;
},0);
}
//add handle
newWindow.onload= winLoaded;
newWindow.onunload = winUnloaded;
}
enter code here
const urls = ['url1', 'url2', 'url3'];
for (url in urls) {
fetch(url)
.then(response => {
return response.text();
})
.then(text => {
console.log(text);
})
}