Reputation: 1540
I've been messing around with Jsoup lately. My friend loves to buy gold for Diablo, so I thought I'd make him a little program that will grab the prices from various websites and present them to him, so he can spend as little money as possible. Usually, I can grab the price like this;
Document Fasteve;
try {
Fasteve = Jsoup.connect("http://www.fasteve.com/diablo-3/Gold/?st=US(Normal)").get();
Elements Price = Fasteve.select("table[class=table_2] tr:eq(5) td:eq(1)");
System.out.println("http://www.fasteve.com/diablo-3/Gold/?st=US(Normal)");
System.out.println("1000M Gold = " + Price.text());
} catch (IOException e) {
e.printStackTrace();
}
However I can't use that method. Nor can I use the method where you state the tr and td you are grabbing from because.. for this site, all the tr's have the same class so I can't call
Elements Price = Fasteve.select("table[class=table] tr[class=row] td:[class=column]");
Any thoughts as to how I can grab that value? (64.37) Thanks once again, Stackoverflow.
Upvotes: 0
Views: 323
Reputation: 285405
Consider
ArrayList<DiabloGoldRow>
, or if you want to be able quickly get information based on the td1 String, a HashMap<String, DiabloGoldRow>
.I'll leave the details of the code as an exercise for the student.
Edit
You ask,
Why do I need to create a separate class to hold the variables?
Because you need to hold the two pieces of information held on each row close together and may need to search on one to obtain the other. It's a lot cleaner to do it this way than to use 2D arrays or parallel arrays. What is your objection towards doing this?
Edit 2
You state,
I am not opposed to anything. I'm simply wondering how that will help me grab the values I need. My question was using the methods I normally do, I cannot grab the data I want to. I was simply looking for a different syntax to grab the specified data.
Again, one way you can do this with a for loop. Simply loop through the rows of the table:
Elements eles = doc.select("table tr");
for (int i = 0; i < eles.size(); i++) {
Elements rowEles = eles.get(i).select("form");
Elements goldEles = rowEles.select("[name=gold]");
String goldValue = goldEles.attr("value");
Elements priceEles = rowEles.select("[name=price]");
String priceValue = priceEles.attr("value");
System.out.printf("%-7s: %-5s%n", goldValue, priceValue);
}
Upvotes: 1