Dr. Mark Humphrys

School of Computing. Dublin City University.

Online coding site: Ancient Brain

coders   JavaScript worlds

Search:

Free AI exercises


Einstein - stock prices


Go on the Internet and get today's stock price.
Usage like:

  
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:
  1. Construct the URL based on the command-line argument.
  2. Use wget to fetch that URL and output to the command line.
  3. 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.

  1. Pipe the above output to tr to delete all new line characters (put the entire file onto a single line).

  2. 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.

  3. grep for "text-4xl font-bold"
    Now you should have just a single line like:
     <div class="text-4xl font-bold ... ">156.47</div>
    

  4. Use sed to remove </div>
  5. 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



ancientbrain.com      w2mind.org      humphrysfamilytree.com

On the Internet since 1987.      New 250 G VPS server.

Note: Links on this site to user-generated content like Wikipedia are highlighted in red as possibly unreliable. My view is that such links are highly useful but flawed.