简体   繁体   中英

Java: how can I use Jsoup to extract a particular data from html?

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 ?

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.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM