首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >通过链接从.csv文件下载文件

通过链接从.csv文件下载文件
EN

Stack Overflow用户
提问于 2015-10-12 15:20:31
回答 3查看 2.8K关注 0票数 3

我有一个包含两列的.csv文件:名称和http链接:

代码语言:javascript
代码运行次数:0
运行
复制
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或您选择的其他工具来完成此操作?

EN

回答 3

Stack Overflow用户

发布于 2015-10-18 02:29:13

在您的示例中,CSV文件中的字段以逗号分隔。

当您调用脚本时,CSV文件的文件名在命令行中给出。

修改脚本权限。

首先在一个临时文件夹中测试它,这样你就不会乱七八糟,如果它不起作用就必须清理。

未测试的

代码语言:javascript
代码运行次数:0
运行
复制
#!/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文件中的两个字段。URL是第一个字段,它将是目录名,f2f1

票数 4
EN

Stack Overflow用户

发布于 2020-01-22 02:15:31

我用python写了一些这样的东西。请记住,您必须首先通过pip install wget安装wget。

代码语言:javascript
代码运行次数:0
运行
复制
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'])
票数 0
EN

Stack Overflow用户

发布于 2017-05-23 21:19:07

代码语言:javascript
代码运行次数:0
运行
复制
  <?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>
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33075347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档