需要从HTML页面解析图像src然后显示它

问题描述 投票:1回答:3

我正在尝试开发一个应用程序,它访问以下站点(http://lulpix.com)并解析HTML并从以下部分获取img src

<div class="pic rounded-8" style="overflow:hidden;"><div style="margin:0 0 36px 0;overflow:hidden;border:none;height:474px;"><img src="**http://lulpix.com/images/2012/April/13/4f883cdde3591.jpg**" alt="All clogged up" title="All clogged up" width="319"/></div></div>

当然每次加载页面时它都是不同的,所以我不能给出我想要做的图像异步库的直接URL,例如

加载页面>解析img src>将ASync下载到imageview>重新加载lulpix.com>重新开始

然后将其中的每一个放置在图像视图中,用户可以从中向左和向右滑动以进行浏览。

所以TL; DR就是这样,我如何解析html来检索URL,并且任何人都有任何libarys用于显示图像的经验。

非常感谢你。

java android html image parsing
3个回答
3
投票

这是一个连接到lulpix的AsyncTask,伪造了一个引用者和用户代理(lulpix试图用一些非常蹩脚的检查来阻止抓取)。在你的Activity中以这样的方式开始:

new ForTheLulz().execute();

生成的Bitmap以相当蹩脚的方式下载(没有缓存或检查图像是否已经是DL:ed)并且错误处理整体上根本不存在 - 但基本概念应该没问题。

class ForTheLulz extends AsyncTask<Void, Void, Bitmap> {
        @Override
        protected Bitmap doInBackground(Void... args) {
            Bitmap result = null;
            try {
                Document doc = Jsoup.connect("http://lulpix.com")
                        .referrer("http://www.google.com")
                        .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                        .get();
                        //parse("http://lulpix.com");
                if (doc != null) {
                    Elements elems = doc.getElementsByAttributeValue("class", "pic rounded-8");
                    if (elems != null && !elems.isEmpty()) {
                        Element elem = elems.first();
                        elems = elem.getElementsByTag("img");
                        if (elems != null && !elems.isEmpty()) {
                            elem = elems.first();
                            String src = elem.attr("src");
                            if (src != null) {
                                    URL url = new URL(src);
                                    // Just assuming that "src" isn't a relative URL is probably stupid.
                                    InputStream is = url.openStream();
                                    try {
                                        result = BitmapFactory.decodeStream(is);
                                    } finally {
                                        is.close();
                                    }
                            }
                        }
                    }
                }
            } catch (IOException e) {
                // Error handling goes here
            }
            return result;
        }
        @Override
        protected void onPostExecute(Bitmap result) {
            ImageView lulz = (ImageView) findViewById(R.id.lulpix);
            if (result != null) {
                lulz.setImageBitmap(result);
            } else {
                //Your fallback drawable resource goes here
                //lulz.setImageResource(R.drawable.nolulzwherehad);
            }
        }
    }

0
投票

我最近使用JSoup来解析无效的HTML,它运行良好!做点什么......

    Document doc = Jsoup.parse(str);
    Element img = doc.body().select("div[class=pic rounded-8] img").first();
    String src = img.attr("src");

玩“选择器字符串”以使其正确,但我认为以上将是有效的。它首先根据其div属性的值选择外部class,然后选择任何后代img元素。


0
投票

现在无需使用webview检查此示例项目

https://github.com/meetmehdi/HTMLImageParser.git

在这个示例项目中,我正在解析html和图像标记,而不是从图像URL中提取图像。图像已下载并显示。

© www.soinside.com 2019 - 2024. All rights reserved.