使用Android中的Jsoup解析html表

时间:2014-05-25 17:45:04

标签: android html json parsing jsoup

我是android编程和使用jsoup的新手。我正在尝试解析html网址并仅复制一对td标记之间的内容,在每行下每行打印时没有原始格式..网址为http://tinyurl.com/63vmnwl。例如,我应该得到像这样的输出:

乙级-德费拉

Arroz /feijãopreto/ arroz integral

Frango assado

Cenoura com ervilha

Saladadealmeirão

Opcional:Quibe de PVT

Goiabinha /如Refresco

Valorcalóricode1refeição1005kcal

我该怎么做?

编辑:对不起,我没有写下部分问题 编辑2:我仍然收到错误@ user184994 这是logcat:

05-25 15:12:56.392: D/(1090): HostConnection::get() New Host Connection established 0xb71f6cc0, tid 1090
05-25 15:12:56.572: W/EGL_emulation(1090): eglSurfaceAttrib not implemented
05-25 15:12:56.572: D/OpenGLRenderer(1090): Enabling debug mode 0
05-25 15:12:58.742: I/dalvikvm(1090): Could not find method org.jsoup.Jsoup.connect, referenced from method com.pcs3100.poliagenda.BandecoActivity.onCreate
05-25 15:12:58.742: W/dalvikvm(1090): VFY: unable to resolve static method 5662: Lorg/jsoup/Jsoup;.connect (Ljava/lang/String;)Lorg/jsoup/Connection;
05-25 15:12:58.742: D/dalvikvm(1090): VFY: replacing opcode 0x71 at 0x000b
05-25 15:12:58.842: D/AndroidRuntime(1090): Shutting down VM
05-25 15:12:58.842: W/dalvikvm(1090): threadid=1: thread exiting with uncaught exception (group=0xb4b00b90)
05-25 15:12:58.852: E/AndroidRuntime(1090): FATAL EXCEPTION: main
05-25 15:12:58.852: E/AndroidRuntime(1090): Process: com.pcs3100.poliagenda, PID: 1090
05-25 15:12:58.852: E/AndroidRuntime(1090): java.lang.NoClassDefFoundError: org.jsoup.Jsoup
05-25 15:12:58.852: E/AndroidRuntime(1090):     at com.pcs3100.poliagenda.BandecoActivity.onCreate(BandecoActivity.java:23)
05-25 15:12:58.852: E/AndroidRuntime(1090):     at android.app.Activity.performCreate(Activity.java:5243)
05-25 15:12:58.852: E/AndroidRuntime(1090):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-25 15:12:58.852: E/AndroidRuntime(1090):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
05-25 15:12:58.852: E/AndroidRuntime(1090):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
05-25 15:12:58.852: E/AndroidRuntime(1090):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
05-25 15:12:58.852: E/AndroidRuntime(1090):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
05-25 15:12:58.852: E/AndroidRuntime(1090):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-25 15:12:58.852: E/AndroidRuntime(1090):     at android.os.Looper.loop(Looper.java:137)
05-25 15:12:58.852: E/AndroidRuntime(1090):     at android.app.ActivityThread.main(ActivityThread.java:4998)
05-25 15:12:58.852: E/AndroidRuntime(1090):     at java.lang.reflect.Method.invokeNative(Native Method)
05-25 15:12:58.852: E/AndroidRuntime(1090):     at java.lang.reflect.Method.invoke(Method.java:515)
05-25 15:12:58.852: E/AndroidRuntime(1090):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
05-25 15:12:58.852: E/AndroidRuntime(1090):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
05-25 15:12:58.852: E/AndroidRuntime(1090):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我认为这就是你的意思:

    String url = "http://www.usp.br/coseas/cardapio.html";

    try {
        Document doc = Jsoup.connect(url).get();
        Elements rows = doc.select("tr pre");

        for (Element elem: rows) {
           System.out.println(elem.text());
        }
    }
    catch (IOException e) {
        e.printStackTrace();
    }

输出是:

SEGUNDA-FEIRA
Arroz/feijão preto/arroz integral
Copa lombo com molho de abacaxi
Virado de milho
Salada de acelga
Opcional: PVT à califórnia
Mexerica/refresco Valor calórico de 1 refeição 1195 kcal