Can we Fetch/Scrape particular data from html site

2019-08-24 15:18发布

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.


I have website it has timestamp in HH:MM AM/PM format.
So this is what is on my website.

Last Updated as of 11:14 pm March 11th

I would like to parse 2:14 PM and compare it(subtract it) with current time stamp, and get the minutes. if difference is greater than 30 mins then I want it do something.

Can someone help me with this? Thanks


I am trying best to understand and modify this code.
FYI Google.com is just example, instead of google it would be link to 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条回答
狗以群分
2楼-- · 2019-08-24 15:46

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.
查看更多
smile是对你的礼貌
3楼-- · 2019-08-24 15:46
@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...
)
查看更多
登录 后发表回答