【问题标题】:Windows batch file to find variable string in a html fileWindows批处理文件在html文件中查找变量字符串
【发布时间】:2016-07-28 12:37:52
【问题描述】:

我正在尝试编写一个 Windows 批处理文件,该文件将查看一个特定的 html 文件,看起来像这样(简化):

            <input name="pattern" value="*.var" type="text" /><img style="width: 16px; height: 16px; vertical-align:middle; cursor:pointer" onclick="this.parentNode.submit()" class="icon-go-next icon-sm" src="/static/474743c8/images/16x16/go-next.png" /></form></div><table class="fileList"><tr><td><img style="width: 16px; height: 16px; " class="icon-text icon-sm" src="/static/474743c8/images/16x16/text.png" /></td><td><a href="./address.var.varapplication-varapplication-varwebservice-05.05.07-SNAPSHOT.var">address.var.varapplication-varapplication-varwebservice-05.05.07-SNAPSHOT.var</a></td><td class="fileSize">133.49 MB</td><td><a href="./address.var.varapplication-varapplication-varwebservice-05.05.07-SNAPSHOT.var/*fingerprint*/"><img style="width: 16px; height: 16px; " class="icon-fingerprint icon-sm" src="/static/474743c8/images/16x16/fingerprint.png" /></a> <a href="./address.var.varapplication-varapplication-varwebservice-05.05.07-SNAPSHOT.var/*view*/">view</a></td></tr><tr><td style="text-align:right;" colspan="3"><div style="margin-top: 1em;"><a href="./*.var/*zip*/target.zip"><img style="width: 16px; height: 16px; " class="icon-package icon-sm" src="/static/474743c8/images/16x16/package.png" />

并使用构建版本(例如 05.05.07-SNAPSHOT - 下一次将是另一个版本,但格式保持不变)作为另一个批处理文件的变量。 我试过 findstr 但没有成功:

for /F "delims=" %%a in ('findstr /ic "webservice" a.html') do set "line=%%a"
set "line=%line:*webservice=%"
for /F "delims=" %%a in ("%line%") do set string=%%a
for %%b in ("%line%") do @ set "var=%%b"
SET build=%var:~-11,8%      
ECHO. %build%

【问题讨论】:

  • 欢迎来到 StackOverflow!您以正确的方式提出问题,包括示例数据、您尝试解析的代码以及清楚地解释您想要的输出。干得好!

标签: html string windows batch-file find


【解决方案1】:

在解析结构化标记时,最好将其视为分层对象而不是平面文本。不仅比尝试将字符串与标记或正则表达式匹配更容易作为层次结构进行导航,而且面向对象的方法也更能抵抗格式的变化(无论代码是缩小、美化、引入换行符,无论)。

考虑到这一点,我建议using a querySelector 选择作为类名为“fileList”的表元素的子元素的锚标记。然后使用正则表达式从锚标记的 href 属性中抓取版本信息。

@if (@CodeSection == @Batch) @then
@echo off & setlocal

set "html=test.html"

for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0" "%html%"') do set "%%I"

echo %build%

goto :EOF
@end // end batch / begin JScript hybrid code

var htmlfile = WSH.CreateObject('htmlfile'),
    fso = WSH.CreateObject('Scripting.FileSystemObject'),
    file = fso.OpenTextFile(WSH.Arguments(0), 1),
    html = file.ReadAll();

file.Close();
htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=9" />' + html);

var anchors = htmlfile.querySelectorAll('table.fileList a');

for (var i = 0; i < anchors.length; i++) {
    if (/webservice-((\d+\.)*\d.+)\.var$/i.test(anchors[i].href)) {
        WSH.Echo('build=' + RegExp.$1);
        WSH.Quit(0);
    }
}

更酷的是,如果您正在抓取的 HTML 文件是由 Web 服务器提供的,您还可以使用 Microsoft.XMLHTTP methods 来检索 HTML,而无需依赖 wgetcurl 或类似的。这只需要对上面的代码做一些小的改动。

@if (@CodeSection == @Batch) @then
@echo off & setlocal

set "URL=http://www.domain.com/file.html"

for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0" "%URL%"') do set "%%I"

echo %build%

goto :EOF
@end // end batch / begin JScript hybrid code

var xhr = WSH.CreateObject('Microsoft.XMLHTTP'),
    htmlfile = WSH.CreateObject('htmlfile');

xhr.open('GET', WSH.Arguments(0), true);
xhr.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
xhr.send('');
while (xhr.readyState != 4) WSH.Sleep(50);

htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=9" />' + xhr.responseText);

var anchors = htmlfile.querySelectorAll('table.fileList a');

for (var i = 0; i < anchors.length; i++) {
    if (/webservice-((\d+\.)*\d.+)\.var$/i.test(anchors[i].href)) {
        WSH.Echo('build=' + RegExp.$1);
        WSH.Quit(0);
    }
}

【讨论】:

  • 第一个代码完美运行,但我非常感兴趣的第二个代码显示以下错误:code url.bat(13, 1) Microsoft JScript runtime error: Object doesn't支持这个属性或方法
  • 如果您能提供一些 cmets 来理解代码,我将不胜感激(我对 JScript 没有任何经验)。谢谢!
  • 我认为 JScript 停止工作,所以我添加了一个代码(在 stackoverflow 上找到它)://to trigger the error: throw new FatalError("Something went badly wrong!"); 并且显示的错误消息是:test.bat(13, 1) Microsoft JScript 运行时错误:发生了严重问题错误的!任何想法为什么会发生这种情况?
  • @Deco LOL 对不起。我在第 13 行有一个错字。我说CreateOjbect。我会修复它。我不想启动网络服务器进行测试,所以如果您发现任何问题,请告诉我。
  • 我不确定发生了什么,因为第一个脚本也停止工作。我重新启动了 Windows,但问题仍然存在。 Java可能有问题吗?我应该重新安装 Java 吗?
【解决方案2】:

试试这个:

findstr /ic:"webservice" a.html

【讨论】:

  • 不幸的是显示:“
  • @ser2956477:我根据您的建议修改了脚本,还添加了“{”,但仍然没有有用的结果:for /F "delims=" %%a in ('findstr /ic:" webservice" test.html') 设置 "line=%%a" 设置 "line=%line:*webservice={%" for /F "delims=" %%a in ("%line%") 设置字符串=%%a for %%b in ("%line%") do @ set "var=%%b" SET build=%var:~-11,8% ECHO。 %build%
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-23
  • 1970-01-01
相关资源
最近更新 更多