douzhi2988 2017-03-24 11:37
浏览 251

使用android中的jsoup库从html页面中提取图像类型

<div class="_jjzlb" style="padding-bottom: 55.2778%;"><img alt="AT Dam party.. #nashik  #big #dam" class="_icyx7" id="pImage_11" src="https://instagram.fbom1-2.fna.fbcdn.net/t51.2885-15/e35/17438694_254543168340407_6435023364997251072_n.jpg" style=""></div>

From above code i have to extract this link- https://instagram.fbom1-2.fna.fbcdn.net/t51.2885-15/e35/17438694_254543168340407_6435023364997251072_n.jpg

How should i extract the link using jsoup library in android ?

I have implemented code as follows.

class RetrieveFeedTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... strings) {

        Document doc = null;
        try {
            doc = Jsoup.connect("https://www.instagram.com/p/BR72iDGg8bAcA73KgP0ZqCopjKfvn5wJy1OQE40/").get();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //Get all elements with img tag ,
        Elements img = doc.getElementsByTag("img");

        String imgSrc = img.attr("src");

        System.out.println("Img source: " + imgSrc);

        return img.toString();
    }

    protected void onPostExecute(String feed) {

        Log.e("MyTag", "Final links ==" + feed);


    }
}

but this code is giving error as--java.lang.NullPointerException: Attempt to invoke virtual method 'org.jsoup.select.Elements org.jsoup.nodes.Document.select(java.lang.String)' on a null object reference

How to solve this?

  • 写回答

1条回答 默认 最新

  • dsagzmosl32217092 2019-05-04 15:16
    关注

    You do not need to log in .

    class RetrieveFeedTask extends AsyncTask<String, Void, String> {
    
        @Override
        protected String doInBackground(String... strings) {
    
            Document doc = null;
            try {
                doc = Jsoup.connect("https://www.instagram.com/p/BR72iDGg8bAcA73KgP0ZqCopjKfvn5wJy1OQE40/").get();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    
            Elements img = doc.select("img");
    
            String imgSrc = img.attr("src");
    
            System.out.println("Img source: " + imgSrc);
    
            return img.toString();
        }
    
        protected void onPostExecute(String feed) {
    
            Log.e("MyTag", "Final links ==" + feed);
    
    
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘