我正在尝试从网页中获取表格,但似乎无法正常工作。

<table cellpadding="0" cellspacing="0" border="0" class="pricetable sortable" id="sortabletable">
    <thead class="tableheader">
        <tr class="sortbottom">
            <th class="thtableheaderlogo unsortable">&nbsp;</th>
            <th class="thtableheaderprice"><div class="tableheaderprice">Pris</div></th>
            <th class="thtableheaderaddress"><div class="tableheaderaddress">Adresse</div></th>
            <th class="thtableheaderobserved unsortable"><div class="tableheaderobserved">Tidspunkt</div></th>
        </tr>
    </thead>
    <tfoot>
        <tr class="unsortable">
            <td colspan="4"><br />* Denne pris er indberettet af selskabet <a style="margin-left: 40px;" href="/indberet">Indberet pris</a></td>
        </tr>
    </tfoot>
    <tbody id="list_canvas">
        <tr>
            <td class="tablebodylogo"><img src="/sites/all/themes/benzinpriser/logo/f24.jpg" alt="" style="width:32px; height: 18px;" /></td>
            <td class="tablebodyprice">&nbsp;<a href="/f24/f24-frederiksborgvej-1" class="octanelink">10.57</a></td>
            <td class="tablebodyaddress" title="Frederiksborgvej 1 3600 Frederikssund">&nbsp;<a href="/f24/f24-frederiksborgvej-1" class="octanelink">Frederiksborgvej 1 3600 Frederikssund</a></td>
            <td class="tablebodydate"><a href="/f24/f24-frederiksborgvej-1" class="octanelink">1 time 57 m </a></td>
        </tr>
        <tr>
            <td class="tablebodylogo"><img src="/sites/all/themes/benzinpriser/logo/q8.gif" alt="" style="width:32px; height: 18px;" /></td>
            <td class="tablebodyprice">&nbsp;<a href="/q8/q8-jernbanegade-43" class="octanelink">10.67</a></td>
            <td class="tablebodyaddress" title="Jernbanegade 43 3600 Frederikssund">&nbsp;<a href="/q8/q8-jernbanegade-43" class="octanelink">Jernbanegade 43 3600 Frederikssund</a></td>
            <td class="tablebodydate"><a href="/q8/q8-jernbanegade-43" class="octanelink">1 time 57 m </a></td>
        </tr>
        <tr>
            <td class="tablebodylogo"><img src="/sites/all/themes/benzinpriser/logo/shell.gif" alt="" style="width:32px; height: 18px;" /></td>
            <td class="tablebodyprice">&nbsp;<a href="/shell/shell-ny-%C3%B8stergade-12" class="octanelink">11.87</a></td>
            <td class="tablebodyaddress" title="Ny Østergade 12 3600 Frederikssund">&nbsp;<a href="/shell/shell-ny-%C3%B8stergade-12" class="octanelink">Ny Østergade 12 3600 Frederikssund</a></td>
            <td class="tablebodydate"><a href="/shell/shell-ny-%C3%B8stergade-12" class="octanelink">1 time 57 m </a></td>
        </tr>
        <tr>
            <td class="tablebodylogo"><img src="/sites/all/themes/benzinpriser/logo/shell.gif" alt="" style="width:32px; height: 18px;" /></td>
            <td class="tablebodyprice">&nbsp;<a href="/shell/shell-askelundsvej-1" class="octanelink">11.87</a></td>
            <td class="tablebodyaddress" title="Askelundsvej 1 3600 Frederikssund">&nbsp;<a href="/shell/shell-askelundsvej-1" class="octanelink">Askelundsvej 1 3600 Frederikssund</a></td>
            <td class="tablebodydate"><a href="/shell/shell-askelundsvej-1" class="octanelink">1 time 57 m </a></td>
        </tr>
        <tr>
            <td class="tablebodylogo"><img src="/sites/all/themes/benzinpriser/logo/circlek.png" alt="" style="width:32px; height: 18px;" /></td>
            <td class="tablebodyprice">&nbsp;<a href="/circle-k/circle-k-servicenter-frederiksv%C3%A6rkvej-16" class="octanelink">10.00</a></td>
            <td class="tablebodyaddress" title="Frederiksværkvej 16 3600 Frederikssund">&nbsp;<a href="/circle-k/circle-k-servicenter-frederiksv%C3%A6rkvej-16" class="octanelink">Frederiksværkvej 16 3600 Frederikssund</a></td>
            <td class="tablebodydate"><a href="/circle-k/circle-k-servicenter-frederiksv%C3%A6rkvej-16" class="octanelink">1 time 57 m </a></td>
        </tr>
    </tbody>
</table>


我想抢表价和地址。

这是我当前的代码。

package com.example.android.soup;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void fetch(View View){
        String sNodes = "";
        TextView text = (TextView) findViewById(R.id.text1234);
        try
        {
            Document doc = Jsoup.parse("http://www.fdmbenzinpriser.dk/searchprices/1/3600");
            System.out.println(doc.getElementById("list_canvas"));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        text.setText(sNodes);
    }
}

最佳答案

parse()将从字符串(https://jsoup.org/cookbook/input/parse-document-from-string)解析文档。您为它传递的URL不是HTML字符串。您必须从URL获取()数据。那就是问题所在。这是一个工作示例:

 Document doc = Jsoup.connect("http://www.fdmbenzinpriser.dk/searchprices/1/3600").get();


      System.out.println(doc.getElementById("list_canvas"));


https://jsoup.org/cookbook/input/load-document-from-url

07-24 09:47
查看更多