【问题标题】:How can i get specific parts inside "href" [duplicate]如何在“href”中获取特定部分 [重复]
【发布时间】:2014-07-17 20:12:56
【问题描述】:

我是 html 解析和尝试获取的新手

document = Jsoup.connect("http://www.beyazperde.com/filmler/tum-filmleri/kullanici-puani/tur-13015/"+"?page=" + i).get();
Elements links = document.select("div.content a.no_underline");
for (Element link : links) 
{
    Element url = link.after("filmler/film-");
    System.out.println(url);
}

当我跑步时,我得到了这些

<a class="no_underline" title="" href="/filmler/film-10080/"> Cesury&uuml;rek </a>
<a class="no_underline" title="" href="/filmler/film-9393/"> Schindler’in Listesi </a>
<a class="no_underline" title="" href="/filmler/film-28359/"> Piyanist </a>

但我想要"10080","9393","28359" 只是这些数字而不是整个&lt;a&gt; 标签。有没有办法做到这一点?

【问题讨论】:

    标签: java html parsing jsoup


    【解决方案1】:

    你可以获取href属性并使用它。

    for (Element link : links) 
    {
        String url = link.attr("href");
        String result = url.split("-")[1].replace("/","");
        System.out.println(result);
    }
    

    【讨论】:

      【解决方案2】:

      如果您将 url 转换为字符串,您可以使用如下正则表达式:

      url.replaceAll(".*href=\"/filmler/film-([0-9]*)/.*","$1");
      

      【讨论】:

        【解决方案3】:
        document = Jsoup.connect("http://www.beyazperde.com/filmler/tum-filmleri/kullanici-        puani/tur-13015/"+"?page=" + i).get();
        Elements links = document.select("div.content a.no_underline");
        for (Element link : links) 
        {
          Attributes attributes = link.attributes();
           String hrefVal = attributes.get("href");
           //use substring or any other logic to get your value
          // Element url = link.after("filmler/film-");
          System.out.println(hrefVal);
        }
        

        【讨论】:

        • 对您的答案稍作修改。这似乎是一个拼写错误:hrfehref :)
        • 感谢 pravin。但是,我无法使用 url.replaceAll(".*href=\"/filmler/film-([0-9]*)/.*", "$1"); Jorge,实际上我不明白代码在做什么。它给了我相同的输出。
        • 你会得到/filmler/film-10080/"/filmler/film-9393//filmler/film-28359/作为输出,你可以使用一些子串逻辑来得到你想要的
        猜你喜欢
        • 2021-07-18
        • 2016-03-29
        • 1970-01-01
        • 2019-09-10
        • 2021-10-11
        • 1970-01-01
        • 1970-01-01
        • 2021-05-16
        • 2018-02-21
        相关资源
        最近更新 更多