Jsoup:需要帮助从元标记中提取内容值

时间:2017-12-11 02:04:30

标签: java html jsoup html-parsing

基本上,我试图打印这个特定硬币的价格。这是我的计划。

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();
        }
    }

}

我希望它输出硬币的当前价格。 但是,当我运行该程序时,它输出:{{selectedCurrency.DATA.PRICE}};什么似乎是一个js变量。有没有办法获得实际价值?

2 个答案:

答案 0 :(得分:0)

你可以尝试我的代码:

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);
    }
}

答案 1 :(得分:0)

您正在寻找一个angularjs模板,并且不包含任何数据。数据正在通过ajax加载。使用网站公开的json端点你会好得多:

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

*请注意,这可能违反网站条款和条件,并且您有责任了解您的法律义务。