簡體   English   中英

我有一個JavaScript數組,我想下載數組中的每個圖像

[英]I have a JavaScript array and I want to download each image in the array

上下文:我的社交媒體上有很多圖片。 我想從社交媒體上下載所有圖像,所以我制作了一個腳本,該腳本獲取所有圖像鏈接並將它們放置在一個數組中(腳本在控制台中執行)。 到目前為止,它僅在Twitter上有效,並且每2秒滾動一次並獲取新鏈接。

我想做的是:我希望能夠在不離開控制台的情況下瀏覽陣列並下載每個圖像文件。 我怎么做? (也可以下載視頻)

我當然搜索了這個問題,但是我的知識是有限的。 我看到了一些有關下載標簽的信息,但它僅適用於html。可能有一種簡單的方法,例如url.download,但我沒有找到

let timePassed = 0 ;
var listOfImages = [];
var videoThumb = "Video Thumb wasn't caught " ;
var timeToWait = 120 ; //You wait 120 before stopping scrolling and loging out the array 

var scroll = setInterval(function() { 
    timePassed += 2; //add 2 seconds to time passed 

    var getImage = document.querySelectorAll(".css-9pa8cd");    //Class that makes mhas the image with the url 

    for (let i = 2; i < getImage.length; i++) {
        let imageUrl = getImage[i].src ; 
        let newStrWithoutSmall  ; 

        if (imageUrl.includes("&name=small")) {
            if ((imageUrl.includes("video_thumb"))) {
                // To catch videos 
                videoThumb = "videoThumb was caught!";
            } else {
                // To catch the images they have &name=small in their url so we delete it 
                newStrWithoutSmall = imageUrl.substring(0, imageUrl.length - 11);
                listOfImages.push(newStrWithoutSmall);
            }
        }
    }

    if (timePassed > timeToWait) {
        clearInterval(scroll);
    }

    window.scrollBy(0,1000); // Scroll down by 1000px and 0on the side 
}, 2000);   //That means every 2 seconds 

var showListImageAndVideos = setTimeout(function() {    
        console.log(listOfImages); // Array of all of the images  
        console.log(videoThumb); // Array of all of the videos 
}, (timeToWait*1000)) //timeToWait

這可能適用於您的情況:

async function downloadFiles(array){
     array.map(async sUrl=>{

    await fetch(sUrl)
      .then(resp => resp.blob())
      .then(blob => {
        var fileName = sUrl.substring(sUrl.lastIndexOf('/') + 1, sUrl.length);
        const url = window.URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.style.display = 'none';
        a.href = url;
        a.download = fileName;
        document.body.appendChild(a);
        a.click();
        window.URL.revokeObjectURL(url);
        document.body.removeChild(a);
      })
      .catch(() => {});
    })
    };

基於: http : //pixelscommander.com/javascript/javascript-file-download-ignore-content-type/ 使用Javascript / jQuery下載文件注意:使用JDownloader之類的下載管理器可能會更好。

在這里,您可以使用async / await使用獲取功能在for循環中依次下載每個圖像。

let timePassed = 0 ;
var listOfImages = [];
var videoThumb = "Video Thumb wasn't caught " ;
var timeToWait = 120 ; //You wait 120 before stopping scrolling and loging out the array 

function toDataURL(url) {
  return fetch(url).then((response) => {
    return response.blob();
  }).then(blob => {
    return URL.createObjectURL(blob);
  });
}

async function download(image) {
  const a = document.createElement("a");
  a.href = await toDataURL(image);
  a.download = image;
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
}

var scroll = setInterval( async function() { 
    timePassed += 2; //add 2 seconds to time passed 

    var getImage = document.querySelectorAll(".css-9pa8cd");    //Class that makes mhas the image with the url 

    for (let i = 2; i < getImage.length; i++) {
        let imageUrl = getImage[i].src ; 
        let newStrWithoutSmall  ; 

        if (imageUrl.includes("&name=small")) {
            if ((imageUrl.includes("video_thumb"))) {
                // To catch videos 
                videoThumb = "videoThumb was caught!";
            } else {
                // To catch the images they have &name=small in their url so we delete it 
                newStrWithoutSmall = imageUrl.substring(0, imageUrl.length - 11);
                listOfImages.push(newStrWithoutSmall);
                await download(newStrWithoutSmall);
            }
        }
    }

    if (timePassed > timeToWait) {
        clearInterval(scroll);
    }

    window.scrollBy(0,1000); // Scroll down by 1000px and 0on the side 
}, 2000);   //That means every 2 seconds 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM