Einstein - stock prices
Go on the Internet and get today's stock price.
Usage like:
- getprice goog
- getprice msft
We will use this site:
Let's hope this site is ok!
If at any point the above link does not give Google/Alphabet's stock price,
put your hand up and let me know.
"View source" and we see that the price is in a section like this:
<div class="text-4xl font-bold ... ">156.47</div>
The bit in dots "..." seems to vary.
Assuming the "text-4xl font-bold" bit is stable,
this div can be identified and extracted.
Note our script may fail in the future if the site changes.
Einstein test environment
As explained before,
in the special Einstein test environment,
we need extension .sh and need to call it with "./" in front of it.
Also for technical reasons we will call it getprice4.
So the conclusion is that we need a file called getprice4.sh
and we run it like this:
./getprice4.sh (arguments)
Getting started
Your program should:
- Construct the URL based on the
command-line argument.
- Use wget
to fetch that URL and output to the command line.
- Pipe the output to tr
to delete all Windows end of line characters.
This is important.
Check with Einstein
Stop right now and check it with Einstein
to make sure you have some marks.
Extract the price
To finish, work through this recipe.
- Pipe the above output to tr
to delete all new line characters (put the entire file onto a single line).
- Use
sed
to put a new line before each
<div
and a new line after each
</div>
For clues, see
how to put new line before and after HTML tags.
- grep for "text-4xl font-bold"
Now you should have just a single line like:
<div class="text-4xl font-bold ... ">156.47</div>
- Use
sed
to remove </div>
- Use sed
to remove everything from start of line to >
You could in fact extract the price in many different ways.
Any method will do, so long as it extracts the price.
Final marks with Einstein
- Upload to Einstein for marking.
-
Possible issue with changing prices:
-
Einstein gets the real price. Then runs your program to make sure it returns the same.
-
There is one issue that could arise:
Trading means the price changes in the sub-second interval between Einstein getting the price
and Einstein running your program.
-
This is probably not why your program is not getting the marks.
There is probably another error.
But just in case,
try running it through Einstein again.