Well, Everyone checks Scores online, what about if we can get the scores or any kind of data that exist on a webpage in HTML format to our Powershell Console.
Yes, It’s Possible – We can retrieve data from web pages to our shell and I’ll guide you how.
First choose a website which displays and updates the live scores at regular frequencues, like
http://www.espncricinfo.com/ci/engine/match/index.html?view=live
Open the URL in Chrome(Browser) and target the score board from which you want to retrieve the scores by right clicking mouse button over it. You will see the Inspect Element option in the pop up menu.
Left click Inspect Element > A window will pop on the Right which has the HTML source code of the website.
Now again, Right click > Inspect Element exactly over the data you want to pull, and the HTML tag in right window will highlight the HTML tag which encloses your data, Like –
<DIV Class=”Innings-Info-1″> Your Data </DIV>
Expand the Tag by clicking it in the right window, to Check the desired data.
Now, You know where your data is and a unique class name that will differentiate this <DIV> tag from others.
OK, So now lets run the the below query in Powershell to get the webpage in our console and store it in a variable
$webpage = Invoke-WebRequest http://www.espncricinfo.com/ci/engine/match/index.html?view=live
and to only get the HTML content of the variable $webpage which holds complete URL’s data, run the below command.
$content = $webpage.Content
Once you have the HTML data, you can search for “<div class=”innings-info-1″>” tag which encloses your data, using a Regular expression query, like below.
$Team1 = ($content | % { [regex]::matches( $_ , ‘(?<=<div class=”innings-info-1″>\s+)(.*?)(?=\s+</div>)’ ) } | select -ExpandProperty value)[1]
$Team1 = $Team1.Replace(‘<span class=”bold”>’,”).replace(‘&’,’&’).split(‘<‘)[0]
The second cmdlet in the above code is there to remove unwanted data (Noise) and to fetch just scores by manipulating the identified string.
Same way you can fetch other data you want with your score like Live commentary, match data available on websites.
COMPLETE SCRIPT :
$webpage = Invoke-WebRequest http://www.espncricinfo.com/ci/engine/match/index.html?view=live $content = $webpage.Content $Team1 = ($content | % { [regex]::matches( $_ , '(?<=<div class="innings-info-1">\s+)(.*?)(?=\s+</div>)' ) } | select -ExpandProperty value)[1] $Team1 = $Team1.Replace('<span class="bold">','').replace('&','&').split('<')[0]$Team2 = ($content | % { [regex]::matches( $_ , '(?<=<div class="innings-info-2">\s+)(.*?)(?=\s+</div>)' ) } | select -ExpandProperty value)[1]$Team2 = $Team2.Replace('<span class="bold">','').split('<')[0] $liveData = (($content | % { [regex]::matches( $_ , '(?<=<div class="match-status">\s+)(.*?)(?=\s+</div>)' ) } | select -ExpandProperty value)[1]) $liveData = $liveData.split('>')[1].split('<')[0] Write-Host -ForegroundColor Yellow "`n`nIndian Premier League - T20`n---------------------------`n`n$Team1 `nVS `n$Team2 `n---------------------------`nMatch Data : $liveData"
Once you run the script it will give you the below output. Super Awesome Isn’t it ? 😉 😀
Well Fun is not over yet, you can convert your data variables into proper HTML again and append the below cmdlet to send it to a Distribution List over the email or You can schedule it in task scheduler to get Live cricket scores in your inbox at set frequency –
Send-MailMessage -to “Your Distribution List” -From ‘Prateek.Singh@labdomain.com’ -Subject “Cricket Score”
-Body ($DataVar| Out-String) -BodyAsHtml -SmtpServer ‘Your SMTP Server’
Well, This script made my day 🙂 hope you enjoy learning as well, Happy reading.
~The only regret was, why didn’t I think of this during the Cricket World Cup 😀
Prateek Singh
Related posts
0 Comments
Leave a Reply to #ChiefGeek_PrateekCancel reply
Categories
Author of Books
Awards
Open Sourced Projects
Author at
Blog Roll
Mike F RobbinsDamien Van Robaeys
Stéphane van Gulick
Kevin Marquette
Adam Bertram
Stephanos Constantinou
Francois-Xavier Cat
Ravikanth Chaganti
Roman Levchenko
Blog Stats
- 1,133,280 People Reached
seems youare html geek bro, not able to understand script
I tried my best make it easy 🙂 , anyways its Powershell scripting and just a bit of HTML.
Nice teheme iwould like touseit in the future
[…] Powershell: How to get Cricket Live Scores to Your Powershell Console […]