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

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

问题描述

我需要使用Windows cmd功能。我需要从网站的两个vars /字符串在批处理脚本中使用它验证动作。为了不让它太简单,此网站需要另外的身份验证。

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!

但是你试图掠夺的脚本似乎是用于检查Web服务器是否响应,而不是获取

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天全站免登陆