javaWeb 批量下载图片

批量下载网页图片

CreateTime--2017年9月26日15:40:43

Author:Marydon

所用技术:javascript、java

测试浏览器:chrome

开发工具:Eclipse

以下载网易云音乐下的图片为例

网页地址:http://music.163.com/#/user/home?id=2952322

第一步:操作javascript

打开控制台(F12)-->选择"console"-->将下面代码粘贴到控制台-->按回车键执行

1.0 版本 

// 获取页面上所有的img标签
var imgTags = document.getElementsByTagName('img');
// 用于临时存储图片地址
var imgAddress = "";
// 对获取的img标签进行迭代
for(var i in imgTags) {
    // img标签存在src属性
    if (undefined != imgTags[i].src) {    
        // 图片地址以逗号形式隔开
        imgAddress += imgTags[i].src + ",";
    }
}
// 去除最后一个逗号
imgAddress = imgAddress.substring(0, imgAddress.length-1);
var textareaTag = document.createElement('textarea');
textareaTag.innerHTML = imgAddress;
document.body.append(textareaTag);

  然后,滚动到网页最下端,复制文本域中的内容 

2.0 版本(推荐使用) 

第一部分:使用js

// 获取页面上所有的img标签
var imgTags = document.getElementsByTagName('img');
// 用于临时存储图片地址
var imgAddress = "";
// 对获取的img标签进行迭代
for(var i in imgTags) {
    // img标签存在src属性
    if (undefined != imgTags[i].src) {    
        // 图片地址以逗号形式隔开
        imgAddress += imgTags[i].src + ",";
    }
}
// 去除最后一个逗号
imgAddress = imgAddress.substring(0, imgAddress.length-1);
// 将图片地址复制到剪贴板上
window.copy(imgAddress);

第二部分:通过java下实现图片下载

将str的值改为剪贴板的内容,运行该java文件即可。

public static void main(String[] args) {  
    String str = "http://p1.music.126.net/p4N3obHWzOm3H_3P7JNKwg==/18501482162620323.jpg, http://p1.music.126.net/4yzvgL7urmYi6nCqFqfDEw==/1402976848734625.jpg, http://p1.music.126.net/OJBT2RGhuNyXD1kNfs6sUw==/18566353348293602.jpg, http://p1.music.126.net/50DKder6jEAyuR4aOJCF1g==/19131502323509750.jpg, http://p1.music.126.net/d5Zs4i0BaAfnOwSTnZqnsw==/19131502323646090.jpg, http://p1.music.126.net/xhX1zfDiUsiKviRWUuQ2vw==/6630055115725896.jpg, http://p1.music.126.net/AYACT-x_Y_Tlhq1Iy5bLaQ==/7703178464485055.jpg, http://p1.music.126.net/eyDtkzThK20RhS3bSK5xQw==/7823025231851652.jpg, http://p1.music.126.net/AoqojLhmjS8qRwAF3D6TvA==/7757054534181455.jpg, http://p1.music.126.net/9Jgo8V0ywBTOU-twYDTfBQ==/2915904837819635.jpg, http://p1.music.126.net/LNyn5DJoc81jCxmyErDaAw==/7991250512717269.jpg, http://p1.music.126.net/GLzrEjb-djsuu-xNrbc00w==/7707576510952544.jpg, http://p1.music.126.net/vCy3mt6Bl3RIrFG30mvKQA==/18711488882741459.jpg, http://p1.music.126.net/SY_7WL71vpQqC4F7H19WLQ==/2935696047329018.jpg, http://p1.music.126.net/AzOG92n2WlKeTg7fq9m7_w==/18747772765758074.jpg, http://p1.music.126.net/JUxJ34J2uxj0JyVWUNJs9Q==/2536573329065725.jpg, http://p1.music.126.net/8kppJUKiT2db029QyqtoeQ==/7819726697159115.jpg, http://p1.music.126.net/YWtHjFbZgNE5ulsVtqRKFg==/3431575790438521.jpg, http://p1.music.126.net/J3EFOzWzUXOAG88rI6K--A==/3407386536531118.jpg, http://p1.music.126.net/3iRvki0JjO81rcdeh4_XlA==/3301833425158964.jpg, http://p1.music.126.net/jijRHCFjBHTXCJ1Zq4QIMQ==/528865152855695.jpg, http://p1.music.126.net/6AET0gegsqiXyryiL6FEAw==/7706477000330775.jpg, http://p1.music.126.net/tOGi0KVAQlbcoZ15K1FuIg==/18668607929575068.jpg, http://p1.music.126.net/5yvktN_nWpQkI7wu6XTA_w==/1382086119808599.jpg, http://p1.music.126.net/ogCtjbkCSBWoPjsE_eGfzw==/908196604543070.jpg, http://p1.music.126.net/sBnYMuTyM_8HXSSQy6SMuw==/2938994581183651.jpg, http://p1.music.126.net/8rfhgWek8JR0RiAGNzmE5g==/7696581395202162.jpg, http://p1.music.126.net/FeHWvb7CJXa7zVW3GKw0Ng==/7875801790315805.jpg, http://p1.music.126.net/Dce8HDvIUhuv_BbPcXNkEA==/2945591652331792.jpg, http://p1.music.126.net/EFPViNtDD6agoko4tNR_XA==/3250156371898995.jpg, http://p1.music.126.net/8ShXfI1Wr48pnMLy-lKvzw==/7968160766516292.jpg, http://p1.music.126.net/S14kBfonaN0XZ1AxNmMxAA==/3287539767114157.jpg, http://p1.music.126.net/-x6ZQZDM_YFGmowhCF3W2w==/7941772487529713.jpg, http://p1.music.126.net/MbsfIOtpsRV9kfmjF0gC9g==/7863707163131262.jpg, http://p1.music.126.net/QILyapoxu5gqptBCTx-OQg==/3359008023072423.jpg, http://p1.music.126.net/mUDvMP2LBMlbzqvGw5Vmbg==/7941772489429945.jpg, http://p1.music.126.net/4w8Qz5jQybKL4mmP11Anwg==/3420580728516916.jpg, http://p1.music.126.net/oQFQwnmYjiqUhxKVmt_ygA==/7978056371794960.jpg, http://p1.music.126.net/9MgPgqGtDr5xnHvfU4Px5A==/2929098977010011.jpg, http://p1.music.126.net/LhcoZ11fGq8YfPxcRpWAoQ==/7985752954599671.jpg, http://p1.music.126.net/YH6HGFHwv1R-5bRUCOBYUQ==/3296335869279681.jpg, http://p1.music.126.net/-uv4dB5beu31ZJWkvyb4mg==/2914805325997916.jpg, http://p1.music.126.net/P74yQEKGsuJwYH-UGIRaEg==/7891194953337864.jpg, http://p1.music.126.net/nCvqNTUwhYSPb6QZYPJTCw==/2918103860292810.jpg, http://p1.music.126.net/2dJ8jJNgvahloRs-s-iq0g==/2442015325316138.jpg, http://p1.music.126.net/vFIWs9m-iebrl_TWpJ8nXA==/7878000813755983.jpg, http://p1.music.126.net/NrjsSRXaMSP6xb0pe9uHDQ==/2909307769624631.jpg, http://p1.music.126.net/l-bTZquInw-hS7iut0cEag==/7856010580801935.jpg, http://p1.music.126.net/6AyE4NYx9-H0LJCObyPRPA==/7823025232221833.jpg, http://p1.music.126.net/bz9sb8unjZWe_BCQh3LUfg==/7816428162226596.jpg, http://p1.music.126.net/j-QDL1REXmDqehyKJvaRRw==/7790039883151880.jpg, http://p1.music.126.net/Fpp8DVKcIQB_YqPu_kukGg==/4438728441341287.jpg, http://p1.music.126.net/YrhUQ_6PzWHCHKx8Adbg4g==/2922501906947842.jpg, http://p1.music.126.net/Sr1EdMTHV79N5K9oMAhjlw==/3396391422112771.jpg, http://p1.music.126.net/wqOH5x32HWkLLZ03ISNj4g==/5652589278466570.jpg, http://p1.music.126.net/8PDmNlJsoe1hLqvBfBqBSw==/2495891395069676.jpg, http://p1.music.126.net/zuKZgz6oLOf48unh7lFnAg==/2913705815055914.jpg, http://p1.music.126.net/KA60jAMZpEtmGVW9BNfbSA==/2332064162536202.jpg, http://p1.music.126.net/AM6q2h9H6RaIo_1eVdWxVA==/925788790612382.jpg, http://p1.music.126.net/AxYqcCZYY5e_Qxa1HK80Gg==/109951162876638337.jpg, http://p1.music.126.net/Jv73MOYc04KUokhA0TRgwQ==/669602581338299.jpg, http://p1.music.126.net/CU2xZQ9-8BESgL2uBs6q3g==/1395280259217310.jpg, http://p1.music.126.net/af9zikWMlImWIEx7KwwGiw==/18537766045982072.jpg, http://p1.music.126.net/EBRqPmY8k8qyVHyF8AyjdQ==/18641120139148117.jpg, http://p1.music.126.net/VQOMRRix9_omZbg4t-pVpw==/18930291695438269.jpg, http://p1.music.126.net/DV3D1d2xv1e5-gKb79hlKw==/3394192402526882.jpg, http://p1.music.126.net/QQvVwj2_A_1dX8z52AkKJQ==/2152843767213633.jpg, http://p1.music.126.net/DPrWUhkBkWeYHZoQmH4wTA==/1365593475037729.jpg, http://p1.music.126.net/yCaY45vN_LzCqzZSaSvKsA==/18589443090869825.jpg, http://p1.music.126.net/7C63iRn_ZrIa927qcFc7Uw==/3385396308536335.jpg, http://p1.music.126.net/gmH-_AkZmnGwLOhPAUqXjQ==/2534374303662413.jpg, http://p1.music.126.net/Qg4uI_x5WLly0IBR6MH0zA==/2912606303233210.jpg, http://p1.music.126.net/qWVdrFNxjsMPm0-_PV8Jcg==/7979155885315048.jpg, http://p1.music.126.net/AA80jM8Vk7p2fEx6UtQq9g==/3087428650874864.jpg, http://p1.music.126.net/myBG4nd2KqgN9vjnYovBQg==/19064432114365606.jpg, http://p1.music.126.net/ka5Nnyund71-Xa3jod0PzA==/7893393976426421.jpg, http://p1.music.126.net/uWVNUhca_6GzkFAyTni2fg==/7704277977099756.jpg, http://p1.music.126.net/-VPM9iI9fd0zRn8yhNY20g==/18743374720708473.jpg, http://p1.music.126.net/7MeQkzzCx30xJ_sJvfZT1A==/5917571581141685.jpg";
    String[] array = str.split(",");
    try{  
        for (int i = 0; i <=array.length; i++) {
            downLoadFromUrl(array[i],i+1 + ".jpg","E:/temp");  
        }
    }catch (Exception e) {  
        // TODO: handle exception  
    }  
}

说明:

  downLoadFromUrl(imgAddress,imgRename,savePath)方法已经封装好,见文章Java读取并下载网络文件

 

posted @ 2017-09-26 15:57  Marydon  阅读(1020)  评论(0编辑  收藏  举报