【问题标题】:Extracting links from all heading tags using jSoup使用 jSoup 从所有标题标签中提取链接
【发布时间】:2015-03-17 17:59:29
【问题描述】:

我正在尝试从网页中存在的所有标题标签<h3> 中提取链接(标题及其地址)。

我试过的代码是:

String u="http://www.thehindu.com/business/";
Document docu = (Document) Jsoup.connect(u).get();

Elements lnk = docu.select("h3");
  for (Element an : lnk) {
      String s= an.attr("abs:href");

        String name = an.text();
        System.out.println( s);

 }

我没有得到任何输出。 有什么问题?

【问题讨论】:

  • What have you tried? 您的代码似乎有什么问题?
  • 按名称 h3 获取所有元素并获取它们的链接属性...
  • @Pshemo 我现在提到了我的代码
  • 你能发布一个小的 HTML 示例来演示这个问题吗?
  • 为什么我的问题被否决了?作为初学者,我想知道我的问题有什么问题,我应该改变吗?

标签: java html-parsing jsoup


【解决方案1】:

您选择了h3 并尝试读取其href 属性,但h3 没有(没有<h3 href="foobar">)。您要选择的是a,它位于h3 中,并从中读取href 值。

所以你的代码应该看起来更像

String u = "http://www.thehindu.com/business/";
Document docu = (Document) Jsoup.connect(u).get();

Elements lnk = docu.select("h3 a[href]");
for (Element an : lnk) {
    String s = an.attr("abs:href");
    String name = an.text();

    System.out.println(name);
    System.out.println(s);
    System.out.println("--------");

}

【讨论】:

  • 对于具有给定 id 名称的特定 div 标签内的所有标题,我该如何做同样的事情?
  • 你需要举一些例子。也可以考虑阅读jsoup.org/cookbook/extracting-data/selector-syntax
  • 我已经完成了我的项目,这是一个使用 jsoup、freetts 和 sphinx 的基于语音的网络浏览器。
猜你喜欢
  • 2018-09-17
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
  • 2019-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多