在批处理文件中使用curl下载网页

时间:2016-06-09 23:06:20

标签: windows batch-file curl scripting

我有兴趣通过在批处理文件的循环中运行curl语句来下载一堆网页。

网页以数字结尾,例如从100到200,我想将每个页面下载为html文件。

当我运行以下程序时,程序会冻结/抛出并出错。我怎样才能让它发挥作用?

提前致谢!

@echo off

echo @echo pages will begin downloading... > ListOfPages.txt

SETLOCAL ENABLEDELAYEDEXPANSION

For /L %%A IN (100,1,200) do (  
    SET page="http://path/to/webpage/%%A"
    SET destination="C:\path\to\file\sitenumber_%%A.html"

    :: insert curl statements to new file
    echo curl !page! -OutFile !destination! >> ListOfPages.txt
)


:: Loop through each line of new file and execute curl statement
For /F %%G in (C:\path\to\file\ListOfPages.txt) do (

     WHAT GOES HERE???

 )

1 个答案:

答案 0 :(得分:0)

答案是你无法运行curl(据我测试过),但你可以在bat文件中使用powershell命令。

Per @llllllllll的建议,将站点列表发送到新文件,但它应该是.bat文件,而不是文本文件。应该使用的命令不是curl,而是PowerShell.exe -Command Invoke-WebRequst

@echo off

echo @echo pages will begin downloading... > ListOfPages.bat

SETLOCAL ENABLEDELAYEDEXPANSION

For /L %%A IN (100,1,200) do (  
    SET page="http://path/to/webpage/%%A"
    SET destination="C:\path\to\file\sitenumber_%%A.html"

    :: insert curl statements to new file
    echo PowerShell.exe -Command Invoke-WebRequest !page! -OutFile !destination! >> ListOfPages.bat
)

然后返回保存.bat文件的文件夹中的PowerShell,然后运行:

./ListOfPages.bat