简体   繁体   中英

Jsoup: need help extracting content value from meta tag

Essentially, I am trying to print the price for this certain coin. Here is my program.

package ZecPrice;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.net.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.*;

public class ZecPrice 
{

    public static void main(String[] args)throws IOException
    {
        URL url1 = new URL("https://www.cryptocompare.com/coins/zec/overview/USD");
        URLConnection Urlconn = url1.openConnection();
         Urlconn.addRequestProperty("User-Agent", "Chrome"); 
        InputStreamReader in = new InputStreamReader(Urlconn.getInputStream());
        BufferedReader buff = new BufferedReader(in);

        String line = buff.readLine();
        while(line != null)
        {
            if(line.contains("<meta itemprop=\"price\""))
            {
              Document doc = Jsoup.parse(line);
              Element meta = doc.select("meta[itemprop=price]").first();
              String content = meta.attr("content");

              System.out.println(content);

            }
            line = buff.readLine();
        }
    }

}

I want it to output the current price of the coin. However when i run the program, it outputs:{{selectedCurrency.DATA.PRICE}}; what seems to be a js variable . Is there any way to get the actual value?

Can you try my code:

public static void main(String[] args)throws IOException
{
    URL url1 = new URL("https://www.cryptocompare.com/coins/zec/overview/USD");
    URLConnection Urlconn = url1.openConnection();
     Urlconn.addRequestProperty("User-Agent", "Chrome"); 
    BufferedReader in = new BufferedReader(new InputStreamReader(
                    url1.getInputStream()));

    String inputLine;
    StringBuffer response = new StringBuffer();
    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    String res = response.toString();
    if(line.contains("<meta itemprop=\"price\"")) {
          Document doc = Jsoup.parse(line);
          Element meta = doc.select("meta[itemprop=price]").first();
          String content = meta.attr("content");
          System.out.println(content);
    }
}

You are looking an an angularjs template and doesn't contain any data. The data is being loading via ajax. You'd be much better off using the json endpoint the website is exposing:

https://min-api.cryptocompare.com/data/histominute?aggregate=10&e=CCCAGG&extraParams=CryptoCompare&fsym=ZEC&limit=144&tryConversion=false&tsym=USD

*Note this may violate the websites terms and conditions and its YOUR responsibility to be aware of your legal obligations.

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