【问题标题】:Download webpages using curl in batch file在批处理文件中使用 curl 下载网页
【发布时间】:2016-06-09 23:06:20
【问题描述】:

我有兴趣通过在批处理文件的循环中运行 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???

 )

【问题讨论】:

  • 对于这种问题,我echo 'curl ...',检查链接是否正常。然后echo 到一个新文件并运行新文件。不要试图在一个脚本中完成。您需要一个检查点管道
  • 这是个好建议。我现在正在尝试。
  • @llllllllllll 一旦我将 curl 语句发送到单独的文本文件,我该如何执行这些 curl 命令? ..我可以更新问题
  • 坦率地说,我不知道如何在 Windows 中做到这一点。你能从 Windows 控制台运行curl 吗?然后,将您的 curl 列表保存为 bat 文件并从 Windows 控制台运行它。它应该工作,但我不确定。如果你使用 Cygwin,那么chmod u+x file; your_shell_name file
  • @llllllllllll 我会尝试你的建议。不幸的是,我不能使用 Cygwin。我一直在处理 Windows 特定的命令。如果我可以使用 linux 命令,这将更容易。简单地说: curl "http://path/to/website/[100-200]" -o sitenumber_#1.html -s

标签: windows batch-file curl scripting


【解决方案1】:

这个问题的答案是你不能运行 curl(据我测试),但是你可以在 bat 文件中使用 powershell 命令。

根据@llllllllllll 的建议,将站点列表发送到一个新文件中,但是它应该是一个 .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

【讨论】:

    猜你喜欢
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2021-07-27
    • 2011-06-04
    • 1970-01-01
    • 2019-12-03
    相关资源
    最近更新 更多