2016-05-04 41 views
0

我试图抓取此网页:http://www.bbc.com/earth/columns/record-breakers。 当我尝试获取所有可用链接时,我的程序仅返回实际链接的一部分。的Java Jsoup提取“ALT”

正如你可以在图片中看到,href属性值包含实际链接的只有一些部分。在网站上,当我将鼠标移到文章上方时,屏幕左下角显示一个小框,并带有正确的链接。

我没有那么多的HTML知识,但我刚刚知道这就是所谓的“alt”属性,所以我的问题是我如何能得到这个信息出现在Jsoup的左边角落?

enter image description here

回答

1

使用ABS:属性前缀来解决从属性绝对URL。上面的页面示例:

public static void main (String []args) throws IOException { 

    Document doc = Jsoup.connect("http://www.bbc.com/earth/columns/record-breakers").get(); 
    Elements link = doc.select("div.promo-unit-header a");  

    for(Element e : link){ 
     System.out.println(e.attr("abs:href"));      
    }  

} 
+0

它现在工作,谢谢:) – imoteb