批处理脚本获取 html 站点并解析内容(无需 wget、curl 或其他外部应用程序) [英] Batch script get html site and parse content (without wget, curl or other external app)

查看:41
本文介绍了批处理脚本获取 html 站点并解析内容(无需 wget、curl 或其他外部应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要使用 windows cmd 功能.我需要来自网站的两个变量/字符串在批处理脚本中使用它来验证操作.为了不让它太简单,这个网站还需要进行身份验证.

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 this website needs authentification in addition.

我在某处找到了这个:

@set @x=0 /*
:: ChkHTTP.cmd
@echo off
setlocal
set "URL=http://www.google.com"
cscript /nologo /e:jscript "%~f0" %URL% | find "200" > nul
if %ErrorLevel% EQU 0 (
echo Web server ok % Put your code here %
) else (
echo Web server error reported
)
goto :EOF

JScript */
var x=new ActiveXObject("Microsoft.XMLHTTP");
x.open("GET",WSH.Arguments(0));x.send();
while (x.ReadyState!=4) {WSH.Sleep(50)};
WSH.Echo(x.status)

但我不确定是否也可以通过这种方式获取网站内容而不是状态答案,而且我更不知道如何对此实施网站身份验证.

But I'm not sure if it's possible to get the site content this way too instead of status answer and the more I don't know how to implement website authentification to this.

上面的代码不能正常工作,因为它总是会因为管道而产生错误,但这似乎更接近我解析我希望的内容的需要.

The above code does not work correctly as it will always produce error because of the pipe, but this seemed nearer to my needs of parsing the content I hoped.

推荐答案

我只使用过 wget 从 Windows 批处理脚本中获取 Web 内容.通过 JScript 使用 XHR 是一个绝妙的主意!

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);

这篇关于批处理脚本获取 html 站点并解析内容(无需 wget、curl 或其他外部应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆