Java 使用jSoup从所有标题标记中提取链接

Java 使用jSoup从所有标题标记中提取链接,java,html-parsing,jsoup,Java,Html Parsing,Jsoup,我正在尝试从网页中存在的所有标题标签中提取链接(标题及其地址) 我尝试过的代码是: 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"); Str

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

我尝试过的代码是:

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);

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

您选择了
h3
,并试图读取其
href
属性,但
h3
没有属性(没有
)。您要选择的是
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("--------");

}

你的代码似乎有什么问题?按名称h3获取所有元素并获取它们的链接属性…@Pshemo我现在提到了我的代码。你能发布一个小的HTML示例来演示这个问题吗?为什么我的问题被否决了?作为一名初学者,我想知道我的问题是否有任何错误,我应该更改这些错误?对于具有给定id名称的特定div标记内的所有标题,我如何可以进行相同的更改?您需要给出一些示例。还考虑阅读我已经完成了我的项目,这是一个基于语音的Web浏览器使用JToice,弗莱特和狮身人面像。