0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

一覧のファイルをcurlで取ってダウンロードするシェルスクリプト書いた

Last updated at Posted at 2018-06-22

やりたいこと

一覧にリンクがベタベタ貼ってあるhtmlから、リンク部分のhtmlファイルを取ってくるために、bashスクリプト書いた。

やったこと

まず、curl http://hogehost/list.html | grep http > list.txtみたいなコマンドを叩いて、リンクがある行及びアドレスを抽出し、

IFS=$'\n'
file=(`cat "$1"`)
ln=0
for line in "${file[@]}"; do
  (( ln++ ))
  name=$(printf hogename.%d.html $ln);
  curl $line -o $name
done```

みたいなシェルを書いて、hoge.shとして保存し、`sh hoge.sh list.txt`として実行することで自動ダウンロードできました。

初めはcurl -Oで取ったんですが、URLにステータス与えて動的htmlを作ってる場合(`http://host?hoge`みたいなの)、ファイル名が"?hoge"になってしまい、その後`mv \?hoge directory/`とかやっても?以降がステータスと見なされてプロトコルエラー出しやがるのでリネームしてます。(`mv \?hoge directory/hoge`みたいにして、ファイル名を変更し、かつそれに?とかの特殊記号が入って無ければフツーに行けるんですけど)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?