Quantcast
Channel: Bashタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 2722

ページ内の画像を一括ダウンロード

$
0
0
#!/bin/bash exec 2> "log.txt" cd `dirname $0` # 引数チェック if [ $# != 1 ]; then echo "第1引数を設定してください ( 複数画像を含む URL )" exit 1 fi target_url=$1 target_html=`curl -Ls $target_url` # 保存用ディレクトリ生成 target_class_id="...保存したい画像を含む ID 属性を設定..." header_title=`echo $target_html | xmllint --html --xpath '//*[@id="${target_class_id}"][1]/h1' - | sed -E "s/<h1>(.+)<\/h1>/\1/"` new_dir="$header_title" mkdir "$new_dir" cd "$new_dir" # ダウンロード curl -Ls $target_url \ | xmllint --html --xpath '//img/@src' - \ | xargs -n 1 \ | cut -d= -f2 \ | sed 's/^\/\//http?s:\/\//' \ | grep -E "\.(png|jpg|jpeg)$" \ > ./tmp.txt image_urls=`cat ./tmp.txt` for image_url in $image_urls do filename=`echo $image_url | sed -E "s/(.+)\/([0-9]*\.)(jpg|png|jpeg)/\2\3/"` echo "Download: $image_url" curl -LO $image_url filesize=`wc -c ./$filename | awk '{print $1}'` echo "filesize: $filesize" sleep 0.2 done rm ./tmp.txt exit 0 curl -L オプション ( --location ) リダイレクト先にもリクエスト可能へ -s オプション ( --silent ) 以下の例のようなダウンロードの進捗状況を非表示 -O オプション ( --remote-name ) ファイルとしてダウンロード ファイル名は URL のうち最後のパスを使用して保存 # 例: `-s` オプション無し ver. % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 470k 100 470k 0 0 3828k 0 --:--:-- --:--:-- --:--:-- 3828k 参考 URL

Viewing all articles
Browse latest Browse all 2722

Trending Articles