javajsoup链接提取

javajsoup链接提取,java,html,jsoup,Java,Html,Jsoup,我试图提取jsoup中给定元素内的链接。以下是我所做但不起作用的事情: Document doc = Jsoup.connect(url).get(); Elements element = doc.select("section.row"); Element s = element.first(); Elements se = s.getElementsByTag("article"); for(Element

我试图提取jsoup中给定元素内的链接。以下是我所做但不起作用的事情:

   Document doc = Jsoup.connect(url).get();
        Elements element = doc.select("section.row");
        Element s = element.first();
        Elements se = s.getElementsByTag("article");


            for(Element link : se){
                System.out.println("link :" + link.select("href"));
            }
以下是html:


我要做的是获取文章类的所有链接。我想,也许首先我必须选择section class=row,然后再从article类派生链接,但我无法使其正常工作。

我在我的一个项目中使用此选项:

final Elements elements = doc.select("div.item_list_section.item_description");
您必须获取要从中提取链接的元素

private static ... inspectElement(Element e) {
        try {
            final String name = getAttr(e, "a[href]");
            final String link = e.select("a").first().attr("href");
            //final String price = getAttr(e, "span.item_price");
            //final String category = getAttr(e, "span.item_category");
            //final String spec = getAttr(e, "span.item_specs");
            //final String datetime = e.select("time").attr("datetime");

            ...
        }
        catch (Exception ex) { return null; }
}

private static String getAttr(Element e, String what) {
    try {
        return e.select(what).first().text();
    }
    catch (Exception ex) { return ""; }
}
试试这个

Document doc = Jsoup.connect(url).get();      

    Elements section = doc.select("#main"); //select section with the id = main
    Elements allArtTags = section.select("article"); // select all article tags in that section
    for (Element artTag : allArtTags ){
        Elements atags = artTag.select("a"); //select all a tags in each article tag
        for(Element atag : atags){
            System.out.println(atag.text()); //print the link text or 
            System.out.println(atag.attr("href"));//print link
        }
    }

结果是什么?上面的代码没有给出任何结果,当我试图打印元素时,它显示的内容与图像中的内容相同,您不需要那些嵌套的循环和选择器。您可以只执行元素atags=doc.selectmain article a。