【问题标题】:Download files by links from .csv file通过 .csv 文件中的链接下载文件
【发布时间】:2015-10-12 07:20:30
【问题描述】:

我有 .csv 文件,它有两列:名称和 http 链接:

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 或您选择的其他工具来完成此操作?

【问题讨论】:

    标签: bash curl wget


    【解决方案1】:

    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 下载多个内容。

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

    【讨论】:

      【解决方案2】:

      我在 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'])
      

      【讨论】:

        【解决方案3】:
          <?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>
        

        【讨论】:

        • 来自审核队列:我可以请求您在源代码周围添加一些上下文。仅代码的答案很难理解。如果您可以在帖子中添加更多信息,这将对提问者和未来的读者都有帮助。
        • 现在这个程序在任何逻辑实现的地方都有 cmets,它会帮助你,还有什么问题请告诉我,顺便谢谢你的关注...
        猜你喜欢
        • 1970-01-01
        • 2016-07-10
        • 1970-01-01
        • 1970-01-01
        • 2014-03-19
        • 2020-03-01
        • 2020-08-05
        • 2010-12-28
        • 1970-01-01
        相关资源
        最近更新 更多