-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYahooFinanceScraper.java
More file actions
84 lines (70 loc) · 2.77 KB
/
YahooFinanceScraper.java
File metadata and controls
84 lines (70 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
public class YahooFinanceScraper {
public static void main(String args[]) throws IOException { //avoids try and catch
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the stock quote abbreviation you wish to check"
+ "(for ex. Apple is AAPL): ");
String stockAbbreviation = scanner.nextLine();
String SA = "https://finance.yahoo.com/quote/" + stockAbbreviation + "?p=" + stockAbbreviation;
//System.out.println(SA);
//Used for when running without scanner
//final String stockName = "REGN";
URL url = new URL(SA);
URLConnection urlConnectionObject = url.openConnection();
InputStreamReader streamInput = new InputStreamReader(urlConnectionObject.getInputStream());
BufferedReader onlineReader = new BufferedReader(streamInput);
String currentPrice = "Price not found";
String HTMLline = onlineReader.readLine();
// Rewrite the Web Scraper parameters
// Gets all of the website information in a different language
// while(HTMLline != null){
// System.out.println(HTMLline);
// HTMLline = onlineReader.readLine();
// }
// Loop to fetch information
// while(HTMLline != null) {
// if(HTMLline.contains("\"StreamDataStore\"")) {
// int indexTarget = HTMLline.indexOf("\"StreamDataStore\"");
// int deciIndex = HTMLline.indexOf(".",indexTarget);
// int startIndex = deciIndex;
// while(HTMLline.charAt(startIndex) != ':') {
// startIndex--;
// }
// currentPrice = HTMLline.substring(startIndex+1, deciIndex+3);
// }
// HTMLline = onlineReader.readLine();
// }
//System.out.println("Current price of " + stockName + "(rounded to nearest cent): " + currentPrice);
while(HTMLline != null) {
if(HTMLline.contains("\"regularMarketVolume\"")) {
int indexTarget = HTMLline.indexOf("\"regularMarketVolume\"");
indexTarget -= 73;
int deciIndex = HTMLline.indexOf(".",indexTarget);
int startIndex = deciIndex;
// Attempts to make the decimals more automated
// if(HTMLline.charAt(deciIndex)!= ',') {
// deciIndex++;
// }
// int deciTwo = HTMLline.indexOf(".",indexTarget);
while(HTMLline.charAt(startIndex) != ':') {
startIndex--;
}
// while(HTMLline.charAt(startIndex) != ',') {
// deciTwo++;
// }
currentPrice = HTMLline.substring(startIndex+1, deciIndex+4);
}
HTMLline = onlineReader.readLine();
}
if(currentPrice.contains(",")) {
int k= currentPrice.indexOf(",");
currentPrice = currentPrice.substring(0,k);
}
System.out.println("Current price of " + stockAbbreviation + ": " + currentPrice);
}
}