通过.csv文件中的链接下载文件

时间:2015-10-12 07:20:31

标签: bash curl wget

我有.csv文件,它有两列:name和http link:

name,link
IN0895,http://sample.com/images/example.jpg
IN0895,http://sample.com/images/example2.jpg
IN0872,http://sample.com/images/name.jpg
IN0872,http://sample.com/images/screen.jpg

我想从第一列创建名称的文件夹,然后从那里下载文件(从第二列开始)。如果文件夹已经存在,只需下载文件并放在那里。

如何使用bash,wget,curl或您选择的其他方式完成此操作?

3 个答案:

答案 0 :(得分:3)

CSV文件中的字段以逗号分隔。

当您调用脚本时,

CSV文件的文件名在命令行中给出。

修改脚本权限。

首先在临时文件夹中对其进行测试,这样就不会弄乱,如果不起作用就必须进行清理。

未经过测试

#!/usr/bin/env bash
filename="$1"
while IFS="," read f1 f2
do
  mkdir -p "$f1";
  wget -P "$f1" "$f2"
done < "$filename"

mkdir -p检查目录是否存在,如果不是

则创建目录

wget -P是要下载的文件夹的前缀(父文件夹),以防您从URL下载多个内容。

f1f2是CSV文件中的2个字段。 f1是第一个将成为目录名称的字段,f2是URL。

答案 1 :(得分:0)

我写了一些在python中做到这一点的东西。请记住,您必须首先通过pip install wget安装wget。

import pandas as pd 
import wget

#read in data
data = pd.read_csv("file.csv") 

# assuming you have a column named Column1 which contains the link, iterate #through and download
for index, row in data.iterrows():
    link = wget.download(row['Column1'])

答案 2 :(得分:-2)

  <?php
  // This is the class which is use for cURL operation..
  class curl_image {
      // Here two variable $name for Rename image, $img_url take image path
      function image($name,$img_url)
      {
         // Here we define a file path where download image will save...
         $path = "E:/xampp/htdocs/beauty_code_image/";

         // Now initialize curl instance here with related method
         $ch = curl_init($img_url);
         $fp = fopen($path . $name, 'wb');
         curl_setopt($ch, CURLOPT_FILE, $fp);
         curl_setopt($ch, CURLOPT_HEADER, 0);

         // cURL excute if above information is right otherwise show error msg
         $result = curl_exec($ch);

         // print_r($result);       it just for display cURL is executed or not
         curl_close($ch);       // Close cURL here
         fclose($fp);
      }
  }

   // Initialize class object here
   $obj = new curl_image();

   // Here we check file is exist
   if(isset($_FILES['file']['name']))
   {
        // We check here data is in valid mentioned format or not
        $csvMimes = array('application/vnd.msexcel','text/plain','text/csv','text/tsv');
        if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$csvMimes)){
            if(is_uploaded_file($_FILES['file']['tmp_name'])){

                 //open uploaded csv file with read only mode
                 $csvFile = fopen($_FILES['file']['tmp_name'], 'r');

                 // fetch csv file here using Php inbuild function                
                 fgetcsv($csvFile);  
                 while(($line = fgetcsv($csvFile)) !== FALSE){
                   // Here object fetch the method which download image & rename it with SKU name
                    $obj->image($line[0].'.jpg',$line[1]);
                  }
             }
             // Close CSV file
             fclose($csvFile);
        }
   }

    ?>

   <html>   
   <head></head>
   <body>
    <div class="panel panel-default">
      <div class="panel-body">
      <form action="" method="post" enctype="multipart/form-data" id="importFrm">
        <input type="file" name="file" />
      <input type="submit" class="btn btn-primary" name="importSubmit" value="IMPORT">
               </form>
           </div>`enter code here`
       </div>
      </body>
   </html>