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 – 下次将是另一个版本,但格式保持不变)作为另一个批处理文件的变量. 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服务器提供,您还可以使用 @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); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Windows窗体中的C#垂直标签
- 当许多计算机通过iSCSI连接到某个卷时,Windows如何工作?
- windows – 如果DHCP服务器在客户端设备之后联机,会发生什么
- windows – 一个简单的程序可以负责BSOD吗?
- 安装了更新2的Windows的Visual Studio Express 2013在“配置
- 如何修复OSX客户端到Windows共享挂起?
- 在Windows中的python选项卡完成
- windows-phone-7 – 在Windows Phone 7中测试连接问题(使用
- windows-7 – 应用程序验证程序安装问题
- .net-3.5 – 在没有DVD的Windows Server 2012上安装Net 3.5