【问题标题】:How to get links from HTML, correct usage of `doc.select`如何从 HTML 中获取链接,正确使用 `doc.select`
【发布时间】:2018-12-11 08:00:22
【问题描述】:

我想从 HTML 代码中获取链接。本页代码:https://www.valorebooks.com/books/fiction/fantasy

 <div class="sub_bar sub_bar_no_pointer"> 
         <span class="showing">Showing 1 - 50 of 28705 - Browse More Fantasy Books for Sale</span> 
         <div class="paginator" id="pg"> 
          <a href="/books/fiction/fantasy" class="active">1</a> 
          <a href="/books/fiction/fantasy?page=2">2</a> 
          <a href="/books/fiction/fantasy?page=3">3</a> 
          <a href="/books/fiction/fantasy?page=4">4</a> 
          <a href="/books/fiction/fantasy?page=5">5</a> 
          <span class="paginatorText">...</span> 
          <a href="/books/fiction/fantasy?page=575">575</a> 
          <span class="paginatorText">|</span> 
          <a href="/books/fiction/fantasy?page=2" class="spriteButton arrow     next icon-right-open"></a>
         </div> 
        </div> 

我找到了一个如何获取链接的示例,但我不确定我应该在doc.select("div.paginator"); 中写什么。它是否正确,也许我应该以另一种方式写这个。

    Elements myLink = doc.select("div.paginator");      

    int number = 0;
    for (Element links : myLink) {
        Elements a = myLink.select("a[href]");                      // get links
        number = Integer.parseInt(a.get(a.size() - 2).text());
    }
    for (int i = 0; i < 20; i++) {
        getData(url + i);
    }
}

【问题讨论】:

    标签: java html jsoup


    【解决方案1】:

    不清楚你想要达到什么目的。

    要打印所有链接的字符串,您可以这样做:

    System.out.println(doc.select("div.paginator").select("a[href]").toString());
    

    对于链接的 Elements 数组:

    Elements theLinks = doc.select("div.paginator").select("a[href]");
    

    【讨论】:

      猜你喜欢
      • 2011-03-05
      • 2021-06-16
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      • 1970-01-01
      • 2015-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多