从span标签android jsoup获取数据

时间:2017-03-14 04:26:39

标签: jsoup

我想从网址示例中获取图片bloger头像: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-000132054558-5ra8gl-t500x500.jpg 谢谢!

1 个答案:

答案 0 :(得分:0)

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

检查页面的查看源。它是 img 标记,而不是 span 标记<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"));
    }             
 }
相关问题