Reputation: 4239
Basically, I am trying to extract the current price of a stock from this link
By looking at the page source, I want to be able to extract the number from this:
<meta itemprop="price"
content="31.40" />
This is my Java code.
public double getCurrentPrice() throws IOException{
String url = "https://www.google.com.hk/finance?q=0023&ei=yF14VYC4F4Wd0ASb64CoCw";
Document doc = Jsoup.connect(url).get();
Element content = doc.getElementById("meta");
}
And I kept getting this error:
456.0Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Element cannot be resolved to a type
at application.Trade.getCurrentPrice(Trade.java:45)
at application.Trade.main(Trade.java:64)
The error message is not very helpful. How should I overcome this ?
Upvotes: 0
Views: 210
Reputation: 9862
import correct classes. also meta is not a id but a tag .so you can't use getElementById
to get that element.using itemprop
attribute get this element and get value by content
attribute .
wildcard only imports classes from the package.for example
import org.jsoup.*
will import org.jsoup.nodes
but not org.jsoup.nodes.Element;
because org.jsoup.nodes.Element
lies in org.jsoup.nodes
package.
example.
import java.io.IOException;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class example {
public static void main(String[] args) throws IOException {
String url = "https://www.google.com.hk/finance?q=0023&ei=yF14VYC4F4Wd0ASb64CoCw";
Document doc = Jsoup.connect(url).get();
Element content = doc.select("meta[itemprop=price]").first();
System.out.println(content.attr("content"));
}
}
output
31.40
edit
to know which classes you should import .....
consider this statement
Document doc
now you are creating Document object so you should import Document class .if you read jsoup api you can see this class hierarchy .
as you can see Document
is a class of package org.jsoup.nodes
so you import class as import org.jsoup.nodes.Document;
.you have to read the api. anyway ides like netbeans,eclipse suggest you some classes to import that's easy and save time a lot.
Upvotes: 1