从跨度标签Android JSOUP获取数据



我想从url示例中获得一个图像博客avatar:https://soundcloud.com/topsify

<span style="background-image: url(&quot;https://i1.sndcdn.com/avatars-000132054558-5ra8gl-t500x500.jpg&quot;); width: 200px; height: 200px; opacity: 1;" class="sc-artwork sc-artwork-placeholder-8 image__rounded image__full g-opacity-transition" aria-label="Topsify’s avatar" aria-role="img"></span>

我尝试得到:

document = Jsoup.connect("https://soundcloud.com/topsify").get();
Elements imgElement = document.select("span[style*=background-image:]");

但它返回空。请支持获取头像网址:https://i1.sndcdn.com/avatars-00013205458-5ra8gl-t500x500.jpg谢谢!

尝试使用userAgent字符串。以下代码只是示例,您可以使用element.select()

检查页面的视图源。它是 img 标签,而不是 span tag <img src="https://i1.sndcdn.com/avatars-000132054558-5ra8gl-t500x500.jpg">

    String url = "https://soundcloud.com/topsify";
    Response res = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko").timeout(3000).execute();
    Document document = res.parse();
    //get <img> tags
    for (Element img : document.getElementsByTag("img")) {
    Elements avatars;
    //get src attribute value whose has "avatars"
    if((avatars = img.getElementsByAttributeValueMatching("src", "avatars")) !=null){
     System.out.println(avatars.attr("src"));
    }             
 }

最新更新