簡體   English   中英

Jsoup:需要幫助從meta標簽提取內容值

[英]Jsoup: need help extracting content value from meta tag

本質上,我正在嘗試打印此特定硬幣的價格。 這是我的程序。

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變量。 有什么方法可以獲取實際值?

你可以試試我的代碼:

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

您正在尋找一個angularjs模板,其中不包含任何數據。 數據正在通過ajax加載。 您最好使用網站公開的json終結點:

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

*請注意,這可能違反網站的條款和條件以及您了解法律義務的責任。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM