【问题标题】:Can't parse image with jsoup android无法使用jsoup android解析图像
【发布时间】:2016-01-08 15:23:03
【问题描述】:

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

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;
}

打开应用程序后,我可以看到文本,但我从ImageLoader 找到默认的image 而不是必需的图像

我的失败在哪里?

【问题讨论】:

  • 您是否正在使用notifyDataSetChanged() 更新适配器?
  • @MatousHybl 我应该在声明适配器(onPostExecute)的方法中这样做吗?

标签: android image html-parsing jsoup


【解决方案1】:

无法使用 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);
}

标准输出

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-21
    • 2014-02-06
    • 2020-02-29
    • 1970-01-01
    • 2013-08-27
    • 2017-12-29
    • 1970-01-01
    • 2012-12-22
    相关资源
    最近更新 更多