【问题标题】:Extracting href from a class within other h3/class classes with jsoup使用 jsoup 从其他 h3/class 类中的类中提取 href
【发布时间】:2017-01-24 13:47:53
【问题描述】:

我正在尝试从 div.class yt-lockup-content 获取 href 属性,但它返回 null 我做错了什么?

<div class="yt-lockup-content">

        <h3 class="yt-lockup-title "><a class="yt-uix-sessionlink yt-uix-tile-link  spf-link  yt-ui-ellipsis yt-ui-ellipsis-2" dir="ltr" title="Live ::   PTV Sports Official Live Transmission"  data-sessionlink="ei=K8uGWMHkGMWjcp7dhNAJ&amp;feature=c4-live-promo" href="/watch?v=8M00cos0d_0">Live ::   PTV Sports Official Live Transmission</a></h3>

到目前为止我尝试过的这段代码:

rssDocument = Jsoup.connect("https://www.youtube.com/channel/UC0KT03NPnN-j4HGzetW9Lpw/featured").timeout(6000).ignoreContentType(true).parser(Parser.htmlParser()).get();
                Elements firstH1 = rssDocument.select("div.yt-lockup-content > h3 > a");

我需要这个网址:href="/watch?v=8M00cos0d_0"

【问题讨论】:

    标签: java android html jsoup


    【解决方案1】:

    一个选项可以是在选择之后读取属性:

    Document doc = Jsoup.parse(html);
    Elements link = doc.select("div.yt-lockup-content > h3 > a");
    String hrefAttribute = link.attr("href");
    System.out.println(hrefAttribute);
    

    输出:

    /watch?v=8M00cos0d_0

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-25
      • 1970-01-01
      相关资源
      最近更新 更多