使用CURL来自动登录并下载东西

要利用curl,关键就是要首先登录,获得cookie,然后才能下载文件。这样就可以利用CURL做很多自动化的脚本或是程序了。关键点是这么几个:

1. 要分析网站的登录form,看需要提供哪些信息。然后组织curl命令行。
2. curl -d表示一个post field。
3. curl -b 指定一个cookie,-c 将得到的cookie写入一个文件。
4. BASH中,Arithmetic expression要用((...)),其他的用[...]
5. BASH中,&&, || 可以用来连接多个条件表达式

以自动下载TorrentGUI, HD001和CHDBits的种子文件为例,脚本是这样的:

# !/bin/bash
# Customize your login info here

TGUSERNAME = xxxxxxx
TGPASSWORD
= xxxxxxxxxxx
HD001USERNAME
= xxxxxxxx
HD001PASSWORD
= xxxxxxxxxxx
CHDUSERNAME
= xxxxxxxxxxx
CHDPASSWORD
= xxxxxxxxxxx

#  Download root directory, DON'T ADD a SLASH at the end of string
DOWNROOTDIR =~/ rtorrent / torrents

#  take care of the arguments
if  [ $ #  -lt 1 ]; then
    echo  " Usage: $0 <tg|hd001|chd> <torrent1 url> <category1> <file rename 1> ... <torrentN url> <categoryN> <file rename N> "
    
exit   1
fi

site
= $ 1
if  [  " $site "   !=   " tg "  ]  &&  [  " $site "   !=   " hd001 "  ]  &&  [  " $site "   !=   " chd "  ]; then
    echo 
" Site must be tg|hd001|chd, Check the command usage. "
    
exit   1
fi
#  Ignore site arg, let's face <torrent url>, <category> and <file rename>
shift

#  Check arguments
declare  - i args_num
let args_num
= $ #
if  ((args_num == 0 ))  ||  ((args_num % 3   !=   0 )) ; then
    echo 
" Torrent URL, category and file rename infos are incorrect. Check the script usage. "
    
exit   1
fi

#  Check whether curl is ready
if  [  - z $(which curl) ]; then
    echo 
" Can't find curl. Install it and try again... "
    
exit   1
fi

#  OK, let's roll
if  [  " $site "   ==   " tg "  ]; then
    echo 
" ====== Login torrentgui... ====== "
    curl 
- d forward = ""   - d jumpurl = " http://torrentgui.com/bbs/index.php "   - d step = 2   - d lgt = 0   - d pwuser = ${TGUSERNAME}  - d pwpwd = ${TGPASSWORD}  - d hideid = 0   - d cktime = 3600   - c tg . cookie  " http://torrentgui.com/bbs/login.php? "
    echo 
" ====== Try to download torrent file... ====== "

    
while  [ $ #  -gt 0 ]; do
        echo  " ====== Handle torrent file $3... ====== "
        curl 
- b tg . cookie  " $1 "   >  ${DOWNROOTDIR} / ${ 2 } / [TorrentGUI]${ 3 } . torrent
        
shift   3
    done

    rm 
- f tg . cookie
fi

if  [  " $site "   ==   " hd001 "  ]; then
    echo 
" ====== Login hd001... ====== "
    curl 
- d username = ${HD001USERNAME}  - d password = ${HD001PASSWORD}  - c hd001 . cookie  " http://www.hd001.org/takelogin.php "
    echo 
" ====== Try to download torrent file... ====== "

    
while  [ $ #  -gt 0 ]; do
        echo  " ====== Handle torrent file $3... ====== "
        curl 
- b hd001 . cookie  " $1 "   >  ${DOWNROOTDIR} / ${ 2 } / [HD001]${ 3 } . torrent
        
shift   3
    done

    rm 
- f hd001 . cookie
fi

if  [  " $site "   ==   " chd "  ]; then
    echo 
" ====== Login CHDBits... ====== "
    curl 
- d username = ${CHDUSERNAME}  - d password = ${CHDPASSWORD}  - c chdbits . cookie  " https://chdbits.org/takelogin.php "
    echo 
" ====== Try to download torrent file... ====== "

    
while  [ $ #  -gt 0 ]; do
        echo  " ====== Handle torrent file $3... ====== "
        curl 
- b chdbits . cookie  " $1 "   >  ${DOWNROOTDIR} / ${ 2 } / [CHD]${ 3 } . torrent
        
shift   3
    done

    rm 
- f chdbits . cookie
fi

echo 
" Done, Bye. "

 

  一点遗憾的是,没找到CURL的option,来自动给出下载文件的文件名。所以脚本中要求使用者自己给下载的文件取个名字。想起来CURL应该是有这样的功能的,哪位要是知道留个comment下来哦。

BTW,请支持PT。

转载于:https://www.cnblogs.com/super119/archive/2010/12/13/1904406.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值