我有一个包含两列的.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或您选择的其他工具来完成此操作?
发布于 2015-10-17 18:29:13
在您的示例中,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文件中的两个字段。URL是第一个字段,它将是目录名,f2
是f1
。
发布于 2020-01-21 18:15:31
我用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'])
发布于 2017-05-23 13:19:07
<?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>
https://stackoverflow.com/questions/33075347
复制相似问题