[英]WGET - how to download embedded pdf's that have a download button from a text file URL list? Is it possible?
[英]how to use wget with Many URL's in .txt file to download and save as
我有一個 txt 文件,其中包含太多要下載的直接鏈接,包括每個 url 前面的文件的每個名稱,txt 文件如下所示:
http://example.com/file1.png name_of_the_file1
http://example.com/file2.mp4 name_of_the_file2
http://example.com/file3.mkv name_of_the_file3
http://example.com/file4.png name_of_the_file4
http://example.com/file5.avi name_of_the_file5
如您所見,文件名和 url 用空格分隔。
我想要的是一個 linux 命令,該命令輸入包含 url 的 txt 文件並下載每個文件,然后使用wget
將它們重命名為各自的名稱。
請幫助我,任何幫助將不勝感激,謝謝!
注1:url 和文件名之間只有一個空格
注意2:文件名可能包含空格:見下面的例子
http://example.com/47188.png Abaixo de Zero (2021)
我能想到的最簡單的就是下面這個簡單的 python 腳本:
import os
lines = open('<name_of_your_file>').readlines()
for line in lines:
url, file_name = line.strip().split(' ', 1)
os.system(f'wget {url} -o {file_name}')
如果想將它放在一個襯里 bash 中,則可以使用以下方法:
$ python -c "import os; lines = open('<name_of_your_file>').readlines(); [ os.system(f'wget {url} -o {file_name}') for url, file_name in [line.strip().split(' ', 1) for line in lines]]"
您可以使用以下代碼:
while IFS= read -r line; do
IFS=' '
read -a strarr <<< "$line"
wget -O ${strarr[1]} ${strarr[0]}
done < filename.txt
這是一個 bash 腳本。 但是如果你不知道如何使用它:
chmod +x file.sh
./file.sh
PS不要忘記更改文件名以使用包含鏈接的實際文件名。
您可以使用這個 awk|xargs 單行:
awk '{url=$1; $1="";{sub(/ /,"");out=url" -O \""$0"\""}; print out}' file.txt | xargs -L 1 wget
解釋:
url=$1 # temp var inside awk
$1="" # replace url with null space
{sub(/ /,"");out=url" -O \""$0"\""} # need to output var
sub(/ /,"") # need to trim leading white space
out=url" -O \""$0"\"" # line formatting with escaping characters
print out # シ
xargs -L 1 wget # get awk output line by line to wget
plus some awk sintax sugar
例子:
cat << EOF >> file.txt
https://www.openssl.org/source/old/1.1.1/openssl-1.1.1k.tar.gz name_of_the_file2
https://www.openssl.org/source/old/1.1.1/openssl-1.1.1j.tar.gz name of_the_file3
https://www.openssl.org/source/old/1.1.1/openssl-1.1.1i.tar.gz name of the_file4
https://www.openssl.org/source/old/1.1.1/openssl-1.1.1h.tar.gz name of the file5
EOF
awk '{url=$1; $1="";{sub(/ /,"");out=url" -O \""$0"\""}; print out}' file.txt | xargs -L 1 wget
ls -1
name_of_the_file2
'name of_the_file3'
'name of the_file4'
'name of the file5'
while IFS= read -r line; do
IFS=' '
read -a strarr <<< "$line"
if [[ ${#strarr[@]} -gt 2 ]]
then
filename=''
for (( i=${#strarr[@]}; i>0; i-- ));
do
filename="${strarr[i]} $filename"
done
wget ${strarr[0]} -O "$filename"
else
wget ${strarr[0]} -O ${strarr[1]}
fi
done < filename.txt
此代碼已修改,現在它能夠創建文件名中包含多個單詞的文件。 但是這段代碼不是很清楚,因為我不知道 bash 腳本的所有功能。 我實際上使用的是 python。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.