i need one answer

Lab 7: Getting on-line stock quotes from Yahoo

This lab is an example of interacting with an HTTP server to retrieve a web page from a system on the network. Here HTML forms are used. HTML forms allow you to type some information in your browser which is sent back to the server for processing. The information may be encoded as part of the URL, or sent separately in name/value pairs.


The Yahoo site is a wide-ranging access portal. They offer online stock quotes that you can read in your browser. A request for a stock quote for ABCD is translated to a socket connection of:


http://finance.yahoo.com/q?s=abcd


That’s equivalent to opening a socket on port 80 of finance.yahoo.com and sending a “get /q?=s=abcd”. You can make that same request yourself, in either of two ways. You can open a socket connection to port 80, the http port (which we have not covered yet!). Or you can open a URL connection which offers a simpler, higher-level interface.


The Yahoo page that returns stock quotes contains thousands of characters of href ads and formatting information. It consists of many HTML lines like this:


<a href=”/q?s=SUNW&d=t”>SUNW</a></td><td nowrap

align=center>12:03PM</td><td nowrap><b>9.55</b><


It is fairly easy to pull out the stock price. From inspecting the output, it’s on a line with more than 25 chars. The line contains the stock symbol rewritten in upper case. The number you want is bracketed by <b>…</b>, which is the HTML to print the number in bold face.


Note: Yahoo site changes each year or once in while the format of its finance webpage. So, you need to figure out what has changed for this assignment and plan your solution accordingly. For 2018, the web site shows the following address for trying to find ibm stock price:


https://finance.yahoo.com/quote/IBM/?p=IBM


So, you need to point the above URL or build the above URL in order to get the yahoo stock quote page for IBM as an example. And your parsing approach would be different as you will find its stock price in a line like shown below:


<span class="Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)" data-reactid="35">129.10</span>

Or, you may also find it in a line with the following pattern:

"currentPrice":{"raw":129.1,"fmt":"129.10"}


Your Lab 7 assignment is to write a program that uses the URL class to create a reference to finance.yahoo.com/quote/StockSymbol as described in the note above. You provide the symbol for a specific stock in your command line. Your program could apply openConnection() method to the URL which underneath creates a socket connection to the URL.

Send the name/value pair (i.e., p=ibm) to the URLConnection as the yahoo site expects. Then read the returned information which is in the format described above. Parse and extract the characters of interest that is the price between data-reactid="35">129.10<. Print out the result on the screen as: ibm is at 129.10.


Please study your textbook, chapter 5, for methods that you can use for a URLConncetion object or use any other online resource. You probably need to use and familiarize yourself with the following methods:


setDoOutput, getOutputStream, and getInputStream