简体   繁体   English

Jsoup,无法从表中获取元素

[英]Jsoup, cannot get an element out of a table

I've been messing around with Jsoup lately. 最近我一直在和Jsoup玩弄。 My friend loves to buy gold for Diablo, so I thought I'd make him a little program that will grab the prices from various websites and present them to him, so he can spend as little money as possible. 我的朋友喜欢为暗黑破坏神购买黄金,所以我想我应该给他制作一个小程序,该程序可以从各种网站上获取价格并将价格呈现给他,这样他就可以花最少的钱。 Usually, I can grab the price like this; 通常,我可以像这样抢价钱。

Document Fasteve;

    try {

        Fasteve = Jsoup.connect("http://www.fasteve.com/diablo-3/Gold/?st=US(Normal)").get();
        Elements Price = Fasteve.select("table[class=table_2] tr:eq(5) td:eq(1)");

        System.out.println("http://www.fasteve.com/diablo-3/Gold/?st=US(Normal)");
        System.out.println("1000M Gold = " + Price.text());

    } catch (IOException e) {
        e.printStackTrace();
    }

However I can't use that method. 但是我不能使用那种方法。 Nor can I use the method where you state the tr and td you are grabbing from because.. for this site, all the tr's have the same class so I can't call 我也不能使用您在其中声明要获取的tr和td的方法,因为..对于此站点,所有tr都具有相同的类,因此我无法调用

Elements Price = Fasteve.select("table[class=table] tr[class=row] td:[class=column]");

我从中获取数据的表

Any thoughts as to how I can grab that value? 关于如何获取该价值有任何想法吗? (64.37) Thanks once again, Stackoverflow. (64.37)再次感谢Stackoverflow。

Consider 考虑

  • Creating a class that holds the td1 String and the td2 or price String, say let's call it DiabloGoldRow or some-such. 创建一个包含td1字符串和td2或price字符串的类,假设我们将其称为DiabloGoldRow或诸如此类。
  • creating an Collection of this class, say, ArrayList<DiabloGoldRow> , or if you want to be able quickly get information based on the td1 String, a HashMap<String, DiabloGoldRow> . 创建一个此类的集合,例如ArrayList<DiabloGoldRow> ,或者如果您希望能够基于td1字符串HashMap<String, DiabloGoldRow>快速获取信息。
  • Then using JSoup to isolate the information in the table, and then iterate through it in a for loop, creating instances of DiabloGoldRow objects and putting them into the ArrayList or other collection (ie, HashMap). 然后使用JSoup隔离表中的信息,然后在for循环中对其进行遍历,创建DiabloGoldRow对象的实例,并将其放入ArrayList或其他集合(即HashMap)。

I'll leave the details of the code as an exercise for the student. 我会将代码的细节留给学生练习。

Edit 编辑
You ask, 你问,

Why do I need to create a separate class to hold the variables? 为什么需要创建一个单独的类来保存变量?

Because you need to hold the two pieces of information held on each row close together and may need to search on one to obtain the other. 因为您需要将每行中保存的两条信息紧密地保持在一起,并且可能需要搜索其中一项以获得另一项。 It's a lot cleaner to do it this way than to use 2D arrays or parallel arrays. 用这种方法比使用2D数组或并行数组要干净得多。 What is your objection towards doing this? 您对此有何异议?

Edit 2 编辑2
You state, 你说

I am not opposed to anything. 我什么都不反对。 I'm simply wondering how that will help me grab the values I need. 我只是想知道这将如何帮助我获得所需的价值。 My question was using the methods I normally do, I cannot grab the data I want to. 我的问题是使用我通常使用的方法,我无法获取想要的数据。 I was simply looking for a different syntax to grab the specified data. 我只是在寻找其他语法来获取指定的数据。

Again, one way you can do this with a for loop. 同样,可以使用for循环执行此操作的一种方法。 Simply loop through the rows of the table: 只需遍历表的各行:

  Elements eles = doc.select("table tr");

  for (int i = 0; i < eles.size(); i++) {
     Elements rowEles = eles.get(i).select("form");

     Elements goldEles = rowEles.select("[name=gold]");
     String goldValue = goldEles.attr("value");

     Elements priceEles = rowEles.select("[name=price]");
     String priceValue = priceEles.attr("value");

     System.out.printf("%-7s: %-5s%n", goldValue, priceValue);
  }

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

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