Batch script get html site and parse content (without wget, curl or other external app)

前端 未结 1 1089

I need to work with windows cmd functionality only. I need two vars/strings from a website to use in the batchscript for validate actions with it. To not make it too simple

相关标签:
1条回答
  • 2020-11-27 06:53

    I've only ever used wget to fetch web content from a Windows batch script. Using an XHR via JScript was a fantastic idea!

    But the script you're trying to plunder appears to be intended for checking whether a web server is responding, not for fetching content.

    With some modifications, you can use it to fetch a web page and do whatever processing you need.

    @if (@a==@b) @end /*
    
    :: fetch.bat <url>
    :: fetch a web page
    
    @echo off
    setlocal
    if "%~1"=="" goto usage
    echo "%~1" | findstr /i "https*://" >NUL || goto usage
    
    set "URL=%~1"
    for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0" "%URL%"') do (
        rem process the HTML line-by-line
        echo(%%I
    )
    goto :EOF
    
    :usage
    echo Usage: %~nx0 URL
    echo     for example: %~nx0 http://www.google.com/
    echo;
    echo The URL must be fully qualified, including the http:// or https://
    goto :EOF
    
    JScript */
    var x=new ActiveXObject("Microsoft.XMLHTTP");
    x.open("GET",WSH.Arguments(0),true);
    x.setRequestHeader('User-Agent','XMLHTTP/1.0');
    x.send('');
    while (x.readyState!=4) {WSH.Sleep(50)};
    WSH.Echo(x.responseText);
    
    0 讨论(0)
提交回复
热议问题