Jsoup不会提取src attr

时间:2015-09-02 18:52:39

标签: android android-asynctask jsoup

 // Title AsyncTask
private class getArticles extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(MainActivity.this);
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            Document document = Jsoup.connect(url).get();
            Elements els = document.select("div.category3-image > a > img");
            System.out.print(els);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        mProgressDialog.dismiss();
    }
}

这样做有效,显示以下项目:

I/System.out﹕ <img width="650" height="400" src="http://www.somesite.net/wp-content/uploads/2015/09/11937978_482236235278145_7661294603539537192_o-650x400.jpg" class="attachment-small-thumb wp-post-image" alt="11937978_482236235278145_7661294603539537192_o-650x400">
09-02 20:43:31.191    2969-3005/net.android I/System.out﹕ <img width="650" height="400" src="http://www.somesite.net/wp-content/uploads/2015/09/Screenshot_16-650x400.jpg" class="attachment-small-thumb wp-post-image" alt="Screenshot_16-650x400">

我尝试将其包装在for(Element e : els)中,检索for函数的内部和外部,将其传递给字符串并使用System.out.print(stringname)

使用了两者,

 String absoluteUrl = els.absUrl("src");  //absolute URL on src

 String srcValue = els.attr("src");

但它不起作用。我尝试在Stackoverflow上搜索,但没有类似的问题。

提前致谢。

1 个答案:

答案 0 :(得分:2)

els属于Elements类型,即Element列表。只有Element有attr()方法。所以试试:

Elements els = document.select("div.category3-image > a > img");
Element el = els.first();
System.out.print(el.attr("src"));