【问题标题】:I have a JavaScript array and I want to download each image in the array我有一个 JavaScript 数组,我想下载数组中的每个图像
【发布时间】:2019-12-26 03:02:49
【问题描述】:

背景:我的社交媒体上有很多图片。我想从我的社交媒体下载所有图像,所以我制作了一个脚本,它获取所有图像链接并将它们放入一个数组中(脚本在控制台中执行)。到目前为止,它只适用于 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

【问题讨论】:

  • 您可以“保存网页,完成”,然后从结果的资产文件夹中获取页面上显示的所有图像。
  • 您提供的代码与问题无关。只需提供一组图像并询问如何从控制台下载每个图像就足以解决您的问题。我相信 node.js 更适合使用无头浏览器和 curl 库下载图像的任务。

标签: javascript arrays image hyperlink download


【解决方案1】:

在这里,您可以使用 async/await 在 for 循环中使用 fetch 顺序下载每个图像。

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 

【讨论】:

    【解决方案2】:

    这可能适用于您的情况:

    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/ Download File Using Javascript/jQuery 笔记: 最好使用 JDownloader 之类的下载管理器。

    【讨论】:

      猜你喜欢
      • 2021-06-17
      • 1970-01-01
      • 2023-01-23
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      • 2014-03-11
      • 2020-11-07
      • 1970-01-01
      相关资源
      最近更新 更多