***JAVA PROBLEM, please help ASAP. In this assignment, you will scrap stock info
ID: 3673023 • Letter: #
Question
***JAVA PROBLEM, please help ASAP.
In this assignment, you will scrap stock information from the Yahoo Finance site. We can read the whole HTML source code as string and parse the resulting string to retrieve the current stock price of a company that is listed on the US stock markets.
For example, you can navigate to the address http://finance.yahoo.com/q?s=AAPL to view the Apple Inc.stock infos. In this case, AAPL is the stock symbol for Apple Inc. For the Alphabet Inc. (a.k.a Google), the stock symbol is GOOG, so you can navigate to http://finance.yahoo.com/q?s=GOOG to read its current stock data.
Your task is to create a program Stock.java that contains functions to display the company name, stock price, volume, Dividend/Yield, and P/E ratio for a company. Given, a stock symbol as command line argument, you are going to scrap the Yahoo finance page for that company to print the above data.
To run your program from a terminal, you can type the following command: java Stock AAPL
The output of your program should look and function like the example shown below. Note, these are realtime data, so you might get different values.
Name: Apple Inc.
Price: 98.12
Volume: 44,831,894
P/E: 10.44
Div/Yield: 2.08 (2.21%)
***My P/E part is already working, I just cannot figure out how to get the proper spacing when dealing with different companies (like AAPL, GOOG, or GOOGL)
Explanation / Answer
// Setup Variables
$stockList = "GOOG,YHOO,T,AAPL";
$stockFormat = "snl1d1t1c1hgw";
$host = "http://quote.yahoo.com/d/quotes.csv";
$requestUrl = $host."?s=".$stockList."&f=".$stockFormat."&e=.csv";
// Pull data (download CSV as file)
$filesize=2000;
$handle = fopen($requestUrl, "r");
$raw = fread($handle, $filesize);
fclose($handle);
// Split results, trim way the extra line break at the end
$quotes = explode(" ",trim($raw));
foreach($quotes as $quoteraw) {
$quoteraw = str_replace(", I", " I", $quoteraw);
$quote = explode(",", $quoteraw);
echo $quote[0]."
"; // output the first element of the array, the Company Name
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.