我认为这可以通过一些 ajax 调用然后寻找那个特定的元素(id="cat")来实现。但我猜这些站点必须位于同一服务器/域上才能正常工作。
我会尝试的另一种方法是创建一个 iframe 并在循环中加载页面并等待 iframe 的 onload 方法,在它加载后我会寻找那个特定的元素并获取它的内容......这更有可能工作,但它会非常缓慢......
var urls = [url1, url2, url3...]; //get all the urls from your file
var urlsLength = urls.length; //get the number of urls to loop for
var iFrame = document.createElement("iframe"); //create an iframe
var iframeContainer = document.getElementById("iframeContainer"); //iframeContainer must exists on your page, you can even hide it with display="none"
var iFrameBody; //variable to hold the iframe body
iframeContainer.appendChild( iFrame ); //add the iframe to its container
for( var i = 0; i<urlsLength; ++i ){ //loop for all the urls
iFrame.src = urls[i]; //browse the designated url
iFrame.onload = function(){ //when it loads, then do your work
iFrameBody = iFrame.contentDocument || iFrame.contentWindow.document; //get the body of the iFrame
doSomething( iFrameBody.getElementById("ELEMENT ID TO LOOK FOR") ); //send the element to your functions
}
}
//this function will receive the element from inside the iframe, you can do whatever you need to
function doSomething(element){
var elementHTML = element.innerHTML;
console.log( element );
}
-编辑-
这种方法太慢了,正如上面在评论中提到的,做这个服务器端是(恕我直言)最好的方法,但至少你有其他选择,我会远离 AJAX 请求在大量 url 上做这样的事情( 30 + ) 并坚持使用 iframe,但仍然相信服务器端是 GO GO。干杯。