Skip to content Skip to sidebar Skip to footer

Can We Fetch/scrape Particular Data From Html Site Into Batch To Do Following?

Awesome work guys by guys in this post on this site. But I need modifying this script according to my needs. I am not hardcore coder but with little help I can get it working.

Solution 1:

Here's a batch script / JScript hybrid using JScript's ability to perform math with dates. Since the web page never specifies, this script assumes the year is this year, so you might get unexpected results running this at the beginning of January if the document was last modified in December. Anyway, here.

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

Example output:

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.

Solution 2:

@echo off
settimeStamp=Last Updated asof2:14 pm March 11th

rem ConverttimeoftimeStampto 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 fromcurrenttimefor/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...
)

Post a Comment for "Can We Fetch/scrape Particular Data From Html Site Into Batch To Do Following?"