简体   繁体   English

Java HTML解析无法获取我的数据?

[英]Java HTML Parsing not getting my data?

I have the following HTML code: 我有以下HTML代码:

<tr class="odd">
    <td class="first name">
      <a href="/quote/III:LN">3i Group PLC</a>
    </td>
    <td class="value">457.80</td>
    <td class="change up">+10.90</td>        <td class="delta up">+2.44%</td>        <td class="value">1,414,023</td>
    <td class="datetime">11:35:08</td>

For which I need to get the data 为此我需要获取数据

457.80 457.80

(ie. The value attribute), and I have this Java code currently: (即value属性),目前我有以下Java代码:

String    FTSE    =            "http://www.bloomberg.com/quote/UKX:IND/members";
    doc = Jsoup.connect(FTSE).get();
    Elements links = doc.select("a[href='/quote/III:LN']");
    for (Element link : links) {

        // get the value from href attribute
        System.out.println("\nlink : " + link.attr("value"));
        System.out.println("text : " + link.text());

When I run my program it terminates having output nothing. 当我运行程序时,它将终止不输出任何内容。 How do I make it so that it outputs the value, which in this case, is '457.80'? 我如何使其输出值(在这种情况下为“ 457.80”)?

links will contain the <a href...> element. links将包含<a href...>元素。 What you are trying to retrieve is the text of a completely different element, ie a <td> tag which has the class value . 您要检索的是完全不同的元素的文本,即具有类value<td>标记。

My guess is that you have multiple <tr> elements and you only want the one which contains the link you've selected. 我的猜测是,您有多个<tr>元素,并且只需要包含所选链接的元素。 In which case you will need the following code: 在这种情况下,您将需要以下代码:

String    FTSE    =            "http://www.bloomberg.com/quote/UKX:IND/members";
doc = Jsoup.connect(FTSE).get();
Elements trs = doc.select("tr:has(a[href='/quote/III:LN'])");
Elements values = trs.select("td.value");
link = values.get(0);
System.out.println("text : " + link.text());

Or something similar... 或类似的东西

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

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