使用Jsoup获取图像

时间:2017-07-24 14:57:25

标签: java android-studio jsoup retrofit2

首先抱歉我的英语不好。我是编程新手(拼贴2年)。我试图从zomato获取图像。是的,API只给我们链接。 (https://www.zomato.com/jakarta/ayam-keprabon-express-tambora/photos)所以我需要从网上手动获取图片。

我已经在iOS中使用Kana完成了这项工作。

func parseHTML(html:String){
    if let doc = Kanna.HTML(html: html, encoding: String.Encoding.utf8){
        for show in doc.css("div[class^='photobox']"){
            let getA = show.css("a").first
            let image = getA!.css("img[class^='res-photo']").first?["data-original"]
            let imageUrl = image!.components(separatedBy: "?")[0]
            linkImageArray.append(imageUrl)
            print("jumlah gambar = \(linkImageArray.count)")
        }
    }
    foodCV.reloadData()
}

我的问题是如何在Jsoup中做到这一点?

这是我如何得到HTML

Call<ResponseBody> photos = client.getPhotos();
    photos.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
            if (response.isSuccessful()){
                    Log.i("retrofit: ","response "+response.body().toString());
                    getPhotos(response.body().toString());
            }else{
                Log.i("retrofit: ","notSuccess");
            }
        }

现在我坚持这个。 Photobox.toString为空。

public void getPhotos(String stringResponse){
    Document doc = Jsoup.parse(stringResponse);
    Elements photobox = doc.getElementsByClass("container");
    Log.i("html : ",""+photobox.toString());
    Toast.makeText(this, photobox.toString(), Toast.LENGTH_SHORT).show();
    for (Element data:photobox){
        Log.i("html : ",""+data.select("img[class^='res-photo']"));
        Log.i("html : ",""+data.select("img[class^='res-photo']").first().select("data-original"));
        data.select("img[class^='res-photo']").first().select("data-original");
    }
}

我确定我不能使用这个Jsoup: how to get an image's absolute url?。它将返回所有图像。我想通过数据原创&#39;来获取img。

<img class='res-photo-thumbnail thumb-load lazy-photo-inner'
        src='https://b.zmtcdn.com/images/photoback.png'
        title='Coffee Sisters - Coffee Sisters&#039;s photo'
        alt='Coffee Sisters - Coffee Sisters&#039;s photo'
        data-original='https://b.zmtcdn.com/data/reviews_photos/e4a/04b8e0669945bfe03fd0789cbd42be4a.jpg?fit=around%7C200%3A200&amp;crop=200%3A200%3B%2A%2C%2A'
        data-type='res'
        data-photo_id='u_NDY5NzEwNzI1MD'
        data-index='27'
        data-category='all'
         />

1 个答案:

答案 0 :(得分:0)

只需改变一些代码行我就能得到我想要的东西〜

Call<ResponseBody> photos = client.getPhotos();
    photos.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
            if (response.isSuccessful()){
                    Log.i("retrofit: ","response "+response.body().toString());
                    getPhotos(response.body());


            }else{
                Log.i("retrofit: ","notSuccess");
            }
        }

我只改为传递response.body,而不是response.body.string.toString。

public void getPhotos(ResponseBody body){
    try {
        Document doc = Jsoup.parse(body.string());
        Elements photobox = doc.select("img[class=res-photo-thumbnail thumb-load lazy-photo-inner]");Toast.LENGTH_SHORT).show();
        for (Element data : photobox) {
            String imagelink = data.attr("data-original").toString();
            String[] parts = imagelink.split("[?]");
            imageList.add(parts[0]);
            Log.i("imagelink : ",parts[0]);
        }
    }catch (IOException e){
        e.printStackTrace();
    }
}