简体   繁体   English

Jsoup:需要帮助从meta标签提取内容值

[英]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}}; 但是,当我运行该程序时,它将输出:{{selectedCurrency.DATA.PRICE}}; what seems to be a js variable . 似乎是一个js变量。 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. 您正在寻找一个angularjs模板,其中不包含任何数据。 The data is being loading via ajax. 数据正在通过ajax加载。 You'd be much better off using the json endpoint the website is exposing: 您最好使用网站公开的json终结点:

https://min-api.cryptocompare.com/data/histominute?aggregate=10&e=CCCAGG&extraParams=CryptoCompare&fsym=ZEC&limit=144&tryConversion=false&tsym=USD 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. *请注意,这可能违反网站的条款和条件以及您了解法律义务的责任。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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