使用了外部工具curl,代码挺繁杂,不会sed等文本提取命令。测试成功率>=80%,失败原因可能找不到歌曲链接,或者无此歌曲。另一个失败原因是只会下载找到的第一个歌曲连接。
以下:- @echo off&Title Batch_Downloader
- if exist temp\ rd temp /s /q 2>nul
- md temp
- set/p name=输入歌曲名字:
- set "nam=%name%"&set "name=%name: =+%"
- curl "temp\_tmp.txt'>http://one.cn.yahoo.com/s?p=%name%&v=music&ei=gbk&pid=ysearch&source=ysmt_topsearch_r&x=">temp\_tmp.txt
- cls
- for /f "tokens=4 delims= " %%a in ('findstr "http://i.music.cn.yahoo.com/down.php?" "temp\_tmp.txt"') do (
- set "str=%%a"
- setlocal enabledelayedexpansion
- set "str=!str:ranges= !"
- for /f %%i in ("!str:~6!") do (
- curl "%%iranges=3">temp\_tmp1.txt&cls
- for /f "tokens=6 delims== " %%s in ('findstr "歌曲出处" "temp\_tmp1.txt"') do (
- echo.&echo.&title %nam%: %%s
- echo ===========================================================
- for /f "skip=1 delims=:" %%d in ('^(echo %%~s^&echo.^)^|findstr/o .*') do set/a m=^(80-%%d-3^)/2
- for /l %%c in (1 1 !m!) do set/p= <nul
- set/p=%%~s<nul&echo.
- echo ===========================================================
- echo.&echo.
- set "ext=%%~s"&set "ext=!ext:~-4!"
- curl %%s -o "%nam%!ext!"
- )
- )
- goto :next
- )
- :next
- rd temp /s /q 2>nul
- for /l %%a in (1 1 4) do echo.
- if exist "%nam%!ext!" (echo 下载完成。) else echo 下载失败。
- pause>nul
复制代码 ------------------------------------------------------------------
10月4日修改:使用WFR.EXE WGET.EXE简化代码,添加歌曲备份功能。- @echo off&setlocal enabledelayedexpansion
- :begin
- echo\&echo\
- for /f "tokens=1*" %%a in ('findstr /v $ %~fs0') do (
- if "%%a" neq "::" (
- set/p var= 输入歌曲存储路径:
- echo\&echo\&set/p "sure= What you specified is !var!,sure?(y/n)"
- if /i "!sure:~,1!" equ "y" (
- (echo.&call set/p=:: %%var%%<nul)>>"%~0"
- ) else (
- cls&goto begin
- )) else (
- set "var=%%b"
- ))
- if not exist "%var%" md "%var%"
- if "%var:~-1%" equ "\" set "var=%var:~,-1%"
- echo.&echo.
- set/p music= 输入歌曲名字:
- set "music=%music: =+%"&set "a=%music%"
- wget -O tmp.txt "http://one.cn.yahoo.com/s?p=%music%&v=music&ei=gbk&pid=ysearch&source=218&x=" -q
- type tmp.txt|wfr /rnnlic:".*(http://i.music.cn.yahoo.com/down.php)" /t:"\1"|wfr /rnnl
- ic:"ranges\=(\d).*" /t:"ranges=\1">t
- move /y t tmp.txt&set/p url=<tmp.txt
- wget "%url%" -O tmp.txt -q
- findstr "歌曲出处" "tmp.txt"|wfr /r:"(.*href\=\"^|\" target.*)" /t>"t"
- echo\&echo\&echo Is downloading...
- move /y t tmp.txt&wget -O "%var%\%a%.mp3" -i tmp.txt -q
- del tmp.txt
- pause
复制代码 wget.exe下载:http://bbs.bathome.net/thread-2040-1-1.html
wfr.exe下载:http://bbs.bathome.net/thread-1909-1-2.html
---------------------------------------------------------------------------------------------------
10月11日更新,添加歌曲试听功能,借助第三方工具curl.exe sed.exe- @echo off&title Puso.D.False
- :: 08-10-11更新,添加歌曲试听、备份功能。
- :: 借助第三方工具sed.exe curl.exe
- :: sed.exe教程+下载:http://bbs.bathome.net/thread-1929-1-1.html
- :: curl.exe下载: http://bbs.bathome.net/thread-1761-1-2.html
- :: 下载以上两个工具,放到此批处理所在目录或%windir%目录中。
- :: 第一次运行需要指定歌曲存储路径,在指定目录下将创建urllist文件夹,用以存储歌曲链接,方便以后使用。
- :: 若要指定其他目录,把此批处理最后一行的":: 上次指定的歌曲路径"删除,重新运行此批处理即可。
- :: 成功率=>90%,出错原因可能是因为文本提取错误,或无此歌曲。
- echo.&echo 初始化…………&echo.&del %temp%\tmp.txt 2>nul
- ping /n 2 nul||(echo">www.baidu.com>nul||(echo 网络出现故障………&pause>nul&exit/b)
- :begin
- setlocal enabledelayedexpansion
- cls&echo\&echo\
- for /f "tokens=1*" %%a in ('findstr /v $ %~fs0') do (
- if "%%a" neq "::" (
- set/p var= 输入歌曲存储路径:
- echo\&echo\&set/p "sure= 你输入的是【!var!】,是否确定(y/n)?"
- if /i "!sure:~,1!" equ "y" (
- (echo.&call set/p=:: %%var%%<nul)>>"%~0"
- ) else (
- cls&endlocal&goto begin
- )) else (
- set "var=%%b"
- ))
- if "%var:~-1%" equ "\" set "var=%var:~,-1%"
- if not exist "%var%\urllist\" md "%var%\urllist"
- cls&echo\&echo\
- set /p music= 输入歌曲名称(x退出):
- if /i "%music%" equ "x" exit/b
- set "a=%music%"&set "music=%music: =+%"
- if exist "%var%\%a%.mp3" (
- cls&echo\&echo\
- set /p "su= 歌曲已经存在,是否继续(y/n)?"
- if /i "!su:~,1!" equ "n" endlocal&goto begin
- )
- if exist "%var%\urllist\%a%.txt" (
- for /f "usebackq tokens=2*" %%i in ("%var%\urllist\%a%.txt") do (set/a y+=1&set "_!y!=%%j")
- goto next
- )
- curl -s "http://one.cn.yahoo.com/s?p=%music%&v=music&ei=gbk&pid=ysearch&source=218&x="|^
- sed "/http.*i\.music\.cn\.yahoo\.com.down\.php/^!d;s/.*\(http.*ranges.[0-9]\).*/\1/">%temp%\tmp.txt
- for /f "delims=" %%i in (%temp%\tmp.txt) do (
- set/a n+=1
- if !n! leq 10 (
- for /f "delims=" %%a in ('curl "%%i" -s^|sed "/歌曲出处.*http.*\.mp3.*/^!d;s/.*\^(http.*mp3\^).*/\1/"') do (
- set/a x+=1
- echo !x! …………^> %%a
- set "_!x!=%%a"
- )) else (
- goto next
- ))>>"%var%\urllist\%a%.txt"
- :next
- cls&echo\&echo\
- type "%var%\urllist\%a%.txt"&echo\&echo\
- set /p num= 输入歌曲编号试听 (e返回,x退出):
- if /i "%num:~,1%" equ "e" endlocal&goto begin
- if /i "%num:~,1%" equ "x" exit/b
- if not defined _%num% goto next
- start wmplayer.exe "!_%num%!"
- echo.&echo.&set /p down= 是否下载(y/n)?
- echo\&echo\
- if /i "%down:~,1%" equ "y" (curl -o "%var%\%a%.mp3" "!_%num%!") else goto next
- del %temp%\tmp.txt 2>nul&endlocal&cls&echo\&echo\
- echo 下载完成,任意键返回……&pause>nul&goto begin
复制代码 链接: https://pan.baidu.com/s/1D_vhFSpy8qc1rqo8EQNNRw?pwd=nvyt
sed教程+下载:http://bbs.bathome.net/thread-1929-1-1.html
curl下载:http://bbs.bathome.net/thread-1761-1-2.html |