无法使用jsoup android解析图像

时间:2015-10-10 21:42:26

标签: android image html-parsing jsoup

我正在接受解析新闻网站的培训。我在解析图像时遇到了一些问题。

http://www.ua-football.com/ - class="stripe-container

来自MainActivity.class的代码

         @Override
           protected Void doInBackground(Void... params){
        arraylist = new ArrayList<HashMap<String, String>>();
        try{
            Document doc = Jsoup.connect(url).userAgent("\"Mozilla/5.0      (Macintosh; U; Intel Mac OS X; de-de) AppleWebKit/523.10.3 (KHTML, like Gecko) Version/3.0.4 Safari/523.10").get();
            for (Element container : doc.select("div[class=stripe-container]")){
                for (Element ul_li : container.select("ul li")){
                    HashMap<String, String> map = new HashMap<String, String>();


                    Elements img_src = ul_li.select("img[src]");
                        String img = img_src.attr("src");
                        System.out.println(img);
                    //map.put("logo", img);
                    map.put("text", ul_li.text());
                    map.put("logo",img);
                    arraylist.add(map);


                }
            }



        } catch (IOException e){
            e.printStackTrace();
        }
        return null;
    }

来自getView

Adapter
 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView text_of_news;
    ImageView logo;

    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View itemView = inflater.inflate(R.layout.list_item, parent, false);
    // Get the position
    resultp = data.get(position);

    text_of_news = (TextView) itemView.findViewById(R.id.text_of_news);
    logo = (ImageView) itemView.findViewById(R.id.imageView);

    text_of_news.setText(resultp.get(MainActivity.TEXT_OF_NEWS));
    imageLoader.DisplayImage(resultp.get(MainActivity.LOGO), logo);



    return itemView;
}

打开应用后,我可以看到文字,但是我发现默认image而不是必需的图片来自ImageLoader

我的失败在哪里?

1 个答案:

答案 0 :(得分:0)

  

无法使用jsoup android

解析图像

要获取图像src,您可能正在寻找类似的内容:

Document doc = Jsoup.connect("http://www.ua-football.com/").timeout(30000).get();
Elements imgs = doc.getElementsByClass("stripe-container").select("img");
for (Element img : imgs) {      
   String imgSrc = img.attr("src");
   System.out.println(imgSrc);
}

<强> STDOUT

http://www.ua-football.com/img/upload/17/21ef0d.jpeg
http://www.ua-football.com/img/upload/17/21d43e.jpeg
http://www.ua-football.com/img/upload/16/2174cb.jpeg
etc...

详细了解jsoup dom-navigation