Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Jsoup创建数组_Java_Arrays_Parsing_Jsoup - Fatal编程技术网

Java 使用Jsoup创建数组

Java 使用Jsoup创建数组,java,arrays,parsing,jsoup,Java,Arrays,Parsing,Jsoup,这是我在这里的第一篇帖子,我真的很感谢大家对我的问题的帮助!基本上,我一直在试图找到一种方法,使用Jsoup元素将每3个元素放入一个单独的数组中,但我被卡住了 我正在制作一个货币转换器,需要三个数组:一个带有货币名称(例如USD),第二个带有转换对(例如USD-EUR),第三个带有反向对(例如EUR-USD) 我运行了下面的代码,并以以下格式列出了一个已删除的转换值列表: USD, ###, ### EUR, ###, ### etc 但我不知道如何将数组填充到三分之一。我试图阅读JSoupA

这是我在这里的第一篇帖子,我真的很感谢大家对我的问题的帮助!基本上,我一直在试图找到一种方法,使用Jsoup元素将每3个元素放入一个单独的数组中,但我被卡住了

我正在制作一个货币转换器,需要三个数组:一个带有货币名称(例如USD),第二个带有转换对(例如USD-EUR),第三个带有反向对(例如EUR-USD)

我运行了下面的代码,并以以下格式列出了一个已删除的转换值列表:

USD, ###, ###
EUR, ###, ###
etc
但我不知道如何将数组填充到三分之一。我试图阅读JSoupAPI,但我是一个完全的初学者,今天还没有取得进展

任何指点都将不胜感激

package jsouptest;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class JsoupTest {

    public static void main(String[] args) {        
        try {
            Document doc = Jsoup.connect("http://www.x-rates.com/table?from=USD&amount=1").userAgent("Safari/11.0.1").get();

            Elements currency = doc.select("td");

            int i=0;
            for (Element names : currency) {
                i++;
                System.out.println(names.getElementsByTag("td").first().text());
            }            

        } catch (IOException ex) {
            Logger.getLogger(JsoupTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

以下方法将起作用:

public static void main(String[] args) {
    try {
        Document doc = Jsoup.connect("http://www.x-rates.com/table?from=USD&amount=1").userAgent("Safari/11.0.1").get();

        Elements currency = doc.select("td");

        List<String> currencyNames = new ArrayList<>();
        List<Double> toRate = new ArrayList<>();
        List<Double> fromRate = new ArrayList<>();

        // this crude scraping assumes that the currency table has the following three columns (ordered from left to right):
        //  - the currency name
        //  - the from rate i.e. the number of units of this currency which can be bought by one dollar
        //  - the to rate i.e. the number of dollars which can be bought by one unit of this currency
        for (int i = 0; i < currency.size(); i += 3) {
            currencyNames.add(currency.get(i).text());
            fromRate.add(Double.valueOf(currency.get(i + 1).select("a").text()));
            toRate.add(Double.valueOf(currency.get(i + 2).select("a").text()));
        }

        // this will output something like:
        //  Currency: Euro, From: 0.857234, To: 1.166543
        System.out.println(String.format("Currency: %s, From: %s, To: %s", currencyNames.get(0), fromRate.get(0), toRate.get(0)));
    } catch (IOException ex) {
        Logger.getLogger(JsoupTest.class.getName()).log(Level.SEVERE, null, ex);
    }
}

同样,您应该包括对每个元素的检查,以确保刮取的HTML文档符合代码的期望

提示:
x%3==0
true
x
为3的倍数时
public static void main(String[] args) {
    try {
        Document doc = Jsoup.connect("http://www.x-rates.com/table?from=USD&amount=1").userAgent("Safari/11.0.1").get();

        Elements currency = doc.select("td");
        Map<String, Double> currencyAndRate = new HashMap<>();
        Elements rates = currency.select("td.rtRates");

        for (Element rate : rates) {
            Elements a = rate.select("a");

            String href = a.attr("href");
            String[] split = href.split("=");
            if (!split[2].equals("USD")) {
                String currencyName = split[2];
                Double value = Double.valueOf(a.text());
                currencyAndRate.put(currencyName, value);
            }
        }

        // this will output something like: 0.857234
        System.out.println(currencyAndRate.get("EUR"));
    } catch (IOException ex) {
        Logger.getLogger(JsoupTest.class.getName()).log(Level.SEVERE, null, ex);
    }
}