我们可以从html站点获取/抓取特定数据到批处理以执行以下操作吗?

时间:2013-03-12 14:57:16

标签: html command-line batch-file batch-processing

this site的帖子中,有很多人都很棒。
但是我需要根据我的需要修改这个脚本。

我不是硬核编码器,但在没什么帮助的情况下我可以让它工作。


我的网站上有HH:MM AM / PM格式的时间戳 这就是我网站上的内容。

Last Updated as of 11:14 pm March 11th

我想解析2:14 PM并将其与当前时间戳进行比较(减去它),然后得到分钟数。如果差异大于30分钟,那么我希望它能做点什么。

有人可以帮我吗?感谢


我正在努力理解和修改此代码 仅供参考 Google.com就是一个例子,而不是谷歌,它将链接到mysite。

@echo off
setLocal EnableDelayedExpansion
set curTimestamp=%date:~7,2% %date:~3,3%_%date:~10,4%_%time:~0,2%_%time:~3,2%
FOR /F "TOKENS=*" %%A IN ('TIME/T') DO SET Now=%%A


for %%F in (q r s t u v w x y z) do if exist %%F del %%F


wget <a href="http://google.com" target="_blank" rel="nofollow">http://google.com</a>

find "Updated" < index.html > q


for /f "tokens=1-4 delims==" %%a in (q) do (
echo %%d >> r
)
for /f "tokens=2-3 delims=>" %%a in (r) do (
echo %%a %%b >> s
)
for /f "tokens=1-2 delims=<" %%a in (s) do (
echo %%a %%b >> t
)
for /f "tokens=1-2 delims=^/" %%a in (t) do (
echo %%a %%b >> u
)
for /f "tokens=1,3 delims= " %%a in (u) do (
echo %%b %%a >> v
)
for /f "tokens=1-4 delims=- " %%a in (v) do (
echo %%c %%b %%a %%d >> w
)
for /f "tokens=* delims= " %%a in (w) do (
set str=%%a
set str=!str:Jan=01!
set str=!str:Feb=02!
set str=!str:Mar=03!
set str=!str:Apr=04!
set str=!str:May=05!
set str=!str:Jun=06!
set str=!str:Jul=07!
set str=!str:Aug=08!
set str=!str:Sep=09!
set str=!str:Oct=10!
set str=!str:Nov=11!
set str=!str:Dec=12!
echo !str! >> x
)
sort < x > y
for /f "tokens=4 delims= " %%a in (y) do (
set AVG=%%a
)
echo wget www.google.com
echo %curTimestamp%
ECHO %Now%
::for %%F in (q r s t u v w x y z) do if exist %%F del %%F
Pause

2 个答案:

答案 0 :(得分:2)

这是一个批处理脚本/ JScript混合使用JScript能够使用日期执行数学运算。由于网页从未指定,因此该脚本假定年份年,因此如果文档在12月最后一次修改,您可能会在1月初运行此结果。无论如何,这里。

@if (@X)==(@Y) @end /* (batch + jscript hybrid script init)

:: *** Batch script *****

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%I in ('wget "%~1" -O- -q 2^>NUL ^| findstr /i "last.*updated.*as.*of"') do (
    for /f "delims=" %%x in ('cscript /nologo /e:jscript "%~f0" "%%I"') do (

        rem test whether date diff >= 30 minutes
        set /a "thirtyMinutes = 30 * 60 * 1000"
        if %%x GEQ !thirtyMinutes! (
            echo Do that voodoo that you do.
        )

        rem Just to demonstrate, you can do some maths to make further sense of the date difference.
        rem set milliseconds=%%x
        set /a "seconds = %%x / 1000, seconds %%= 60"
        set /a "minutes = %%x / 1000 / 60, minutes %%= 60"
        set /a "hours = %%x / 1000 / 60 / 60, hours %%= 24"
        set /a "days = %%x / 1000 / 60 / 60 / 24"
        echo %~1 last modified !days! days !hours! hours !minutes! minutes ago.
    )

    rem Once the for loop has fired, exit.
    exit /b
)

rem In case the web page does not contain "last updated as of"
exit /b


:: *** JScript script *****/
var args = [];
for (var i=0; i<WScript.arguments.length; i++) { args.push(WScript.arguments(i)) }
var t = args.join(' ').replace(/^\s+|<[^>]+>|\s+$/g,'').replace(/\&nbsp;/g, ' ').split(' ');
var h = t[4].split(':')[0];
if (/pm/i.test(t[5])) h = h * 1 + 12;
var ds = t[6] + ' ' + t[7] + ', ' + new Date().getFullYear() + ' ' + h + ':' + t[4].split(':')[1];
var diff = new Date() - new Date(ds);
WScript.echo(diff);

示例输出:

C:\Users\me\Desktop>test.bat http://stackoverflow.com/questions/15364653/
Do that voodoo that you do.
http://stackoverflow.com/questions/15364653/ last modified 0 days 23 hours 18 minutes ago.

答案 1 :(得分:1)

@echo off
set timeStamp=Last Updated as of 2:14 pm March 11th

rem Convert time of timeStamp to number of minutes
for /F "tokens=5-7 delims=: " %%a in ("%timeStamp%") do (
   set /A minutes=%%a*60 + 1%%b%%100
   if %%c equ pm set /A minutes+=12*60
)

rem Subtract that number of minutes from current time
for /F "tokens=1-2 delims=:" %%a in ("%time%") do (
   set /A minutes=%%a*60 + 1%%b%%100 - minutes
)

rem If time lapse is negative, timeStamp was taken from previous day
if %minutes% lss 0 (
   set /A minutes+=24*60
)

if %minutes% gtr 30 (
   echo Do something...
)