简体   繁体   中英

How can I get the data from website in Java?

I want to get the value of "Yield" in " http://www.aastocks.com/en/ltp/rtquote.aspx?symbol=01319 " How can I do this with java?

I have tried "Jsoup" and my code like these:

public static void main(String[] args) throws IOException {

    String url = "http://www.aastocks.com/en/ltp/rtquote.aspx?symbol=01319";
    Document document = Jsoup.connect(url).get();

    Elements answerers = document.select(".c3 .floatR ");
    for (Element answerer : answerers) {
        System.out.println("Answerer: " + answerer.data());
    }
    // TODO code application logic here
}

But it return empty. How can I do this?

Your code is fine. I tested it myself. The problem is the URL you're using. If I open the url in a browser, the value fields (eg Yield) are empty. Using the browser development tools (Network tab) you should get an URL that looks like:

http://www.aastocks.com/en/ltp/RTQuoteContent.aspx?symbol=01319&process=y


Using this URL gives you the wanted results.

The simplest solution is to create a URL instance pointing to the web page / link you want get the content using streams-

for example-

public static void main(String[] args) throws IOException 
{

    URL url = new URL("http://www.aastocks.com/en/ltp/rtquote.aspx?symbol=01319");

    // Get the input stream through URL Connection
    URLConnection con = url.openConnection();
    InputStream is =con.getInputStream();

    // Once you have the Input Stream, it's just plain old Java IO stuff.

    // For this case, since you are interested in getting plain-text web page
    // I'll use a reader and output the text content to System.out.

    // For binary content, it's better to directly read the bytes from stream and write
    // to the target file.


    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    String line = null;

    // read each line and write to System.out
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
}

I think Jsoup is critical in this purpose. I would not suspect a valid HTML document (or whatever).

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