使用JSOUP在android中解析HTML

时间:2012-11-25 15:51:02

标签: android html-parsing jsoup

我正在尝试从网站解析一些HTML。我连接到AsyncTask中的网站。我尝试使用JSOUP解析器解析检索到的HTML。

website= task.get();
            String items="";
            Document doc = Jsoup.parse(website);
            Element content = doc.getElementById("rtable");
            Elements links = content.getElementsByTag("tr");
            for (Element link : links) {
             items=items+ link.child(2).text()+link.child(3).text()+ link.child(0).getElementsByTag("a").attr("href");

            }
            mDateDisplay.setText(items);

如果我在普通的java项目中解析html它可以正常工作但不在Android项目中。我在行中得到一个空指针异常:

Element content = doc.getElementById("rtable");

输出错误:

11-25 16:42:30.674: W/System.err(972): java.lang.NullPointerException
11-25 16:42:30.684: W/System.err(972):  at com.example.seminarska.Prevozi.onCreate(Prevozi.java:89)
11-25 16:42:30.684: W/System.err(972):  at android.app.Activity.performCreate(Activity.java:5008)
11-25 16:42:30.684: W/System.err(972):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-25 16:42:30.684: W/System.err(972):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-25 16:42:30.694: W/System.err(972):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-25 16:42:30.694: W/System.err(972):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-25 16:42:30.694: W/System.err(972):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-25 16:42:30.731: W/System.err(972):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 16:42:30.734: W/System.err(972):  at android.os.Looper.loop(Looper.java:137)
11-25 16:42:30.734: W/System.err(972):  at android.app.ActivityThread.main(ActivityThread.java:4745)
11-25 16:42:30.761: W/System.err(972):  at java.lang.reflect.Method.invokeNative(Native Method)
11-25 16:42:30.764: W/System.err(972):  at java.lang.reflect.Method.invoke(Method.java:511)
11-25 16:42:30.764: W/System.err(972):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-25 16:42:30.764: W/System.err(972):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-25 16:42:30.774: W/System.err(972):  at dalvik.system.NativeStart.main(Native Method)

哦,我想解析的网站是this

1 个答案:

答案 0 :(得分:1)

String url = "http://www.google.com";
List<String> images = new ArrayList<String>();
Document doc = Jsoup.connect(url).get();
Elements img = doc.select("img");
for (Element el : img)
{
    String imageUrl = el.attr("src");
    images.add(imageUrl);
}