【问题标题】:Android jSoup HelpAndroid jSoup 帮助
【发布时间】:2011-09-02 08:28:48
【问题描述】:

我正在尝试使用 jSoup 从这个 url http://skyalipi.blogspot.com/2011/04/there-is-no-resistance-without.html 解析 html。我正在使用此代码

Document doc = Jsoup.parse("http://skyalipi.blogspot.com/2011/04/there-is-no-resistance-without.html");  

       Log.d("test", "the elements"+doc);

在日志中,我得到以下内容

05-26 12:05:05.355: DEBUG/test(696): the elements<html>
05-26 12:05:05.355: DEBUG/test(696):  <head></head>
05-26 12:05:05.355: DEBUG/test(696):  <body>
05-26 12:05:05.355: DEBUG/test(696):   http://skyalipi.blogspot.com/2011/04/there-is-no-resistance-without.html 
05-26 12:05:05.355: DEBUG/test(696):  </body>
05-26 12:05:05.355: DEBUG/test(696): </html>

我想获取段落内容。我不知道我哪里出错了。我也参考了以下网址http://jsoup.org/cookbook/extracting-data/attributes-text-html请帮助

【问题讨论】:

    标签: android html-parsing jsoup


    【解决方案1】:

    Jsoup 将您的 URL 视为您要解析的文本,并将其转换为有效的 HTML 以便可以对其进行解析。我认为您想连接到该站点并检索该 url 的内容,然后解析结果:

    Document doc = Jsoup.connect("http://skyalipi.blogspot.com/2011/04/there-is-no-resistance-without.html").get();
    

    编辑

    查看the documentation 的示例。您可以执行以下操作:

    Element example = doc.getElementById("alternatives1");
    Log.d("test","example "+example.text());
    

    【讨论】:

    • @dave.c 感谢您的快速回复是的,我也尝试过连接。你能告诉我上述声明之后 doc 的内容是什么,以及如何从那里提取标签。请帮忙
    • @Droidme 添加链接和小例子
    • @dave.c 我试过了。但我收到以下错误 05-26 13:04:42.615: ERROR/AndroidRuntime(994): Caused by: java.lang.NullPointerException 05-26 13:04:42.615: ERROR/AndroidRuntime(994): at slots.jsoup .soup.onCreate(soup.java:33)
    • @dave.c 第 33 行是 Element example = doc.getElementById("alternatives1");请帮忙
    • @Droidme 然后doc 为空。你真的打电话给get()吗?你有互联网权限吗?
    猜你喜欢
    • 1970-01-01
    • 2021-02-24
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    相关资源
    最近更新 更多