加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

Windows批处理文件,用于在html文件中查找变量字符串

发布时间:2020-12-14 05:31:59 所属栏目:Windows 来源:网络整理
导读:我正在尝试编写一个 Windows批处理文件,它将查看特定的html文件,看起来像这样(简化): input name="pattern" value="*.var" type="text" /img style="width: 16px; height: 16px; vertical-align:middle; cursor:pointer" onclick="this.parentNode.submit()
我正在尝试编写一个 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%

解决方法

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

考虑到这一点,我建议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,而不必依赖于wget或curl或类似的.这只需要对上面的代码进行一些小的更改.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读