Jsoup框架爬取网络图片

一、先在build.gradle中导入这两个框架,一个是jsoup(爬虫),一个是picasso(加载网络图片),一个是列表控件recycleview;导入后ctrl+f9

dependencies {
    implementation 'org.jsoup:jsoup:1.9.2'
    implementation 'com.squareup.picasso:picasso:2.5.2'
    implementation 'com.android.support:recyclerview-v7:28.1.1'

二、查看我们要爬取的页面的效果图,登录http://www.jj20.com/

三、代码展示:

1.布局(activity_demo.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".DemoActivity">
  <TextView
      android:layout_marginBottom="10dp"
      android:gravity="center|left"
      android:layout_marginTop="10dp"
      android:textStyle="bold"
      android:textSize="18sp"
      android:textColor="#ffffff"
      android:text="最新壁纸"
      android:background="#51b6ec"
      android:layout_width="150dp"
      android:layout_height="40dp" />
    <androidx.recyclerview.widget.RecyclerView
        android:background="#2fb9a6af"
        android:id="@+id/rv_demo1"
        android:layout_width="match_parent"
        android:layout_height="200dp"></androidx.recyclerview.widget.RecyclerView>
<View
    android:layout_width="match_parent"
    android:layout_height="3dp"/>
  <TextView
      android:layout_marginBottom="10dp"
      android:gravity="center|left"
      android:layout_marginTop="10dp"
      android:textStyle="bold"
      android:textSize="18sp"
      android:textColor="#ffffff"
      android:text="风景壁纸"
      android:background="#51b6ec"
      android:layout_width="150dp"
      android:layout_height="40dp" />
  <androidx.recyclerview.widget.RecyclerView
      android:background="#2fb9a6af"
      android:id="@+id/rv_demo2"
      android:layout_width="match_parent"
      android:layout_height="200dp"></androidx.recyclerview.widget.RecyclerView>

2.jsoup工具类(JsoupUtil.java)

public class JsoupUtil {
    private static JsoupUtil util;


    public static JsoupUtil getInstance() {
        if (util == null) {
            util = new JsoupUtil();
        }
        return util;
    }

    public void getPhotoLists1(final Handler handler, final String url) {
        final List<PhotoList> oList=new ArrayList<>();
        new Thread(){

            @Override
            public void run() {
                super.run();
                Connection connection=Jsoup.connect(url);
                connection.timeout(1000);
                try {
                    Document document=connection.get();

                    Element ul=document.select("ul.picbz").first();
                    Elements lis=ul.getElementsByTag("li");
                    for (Element li:lis
                            ) {
                        Element a=li.getElementsByTag("a").first();
                        String title=a.attr("title");
                        String imgUri=a.getElementsByTag("img").attr("src");
                        PhotoList photoList=new PhotoList(imgUri,title);
                        oList.add(photoList);
                    }

                    Message message=handler.obtainMessage();
                    message.what=0x33;
                    message.obj=oList;
                    handler.sendMessage(message);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    public void getPhotoLists2(final Handler handler, final String url) {
        final List<PhotoList> oList=new ArrayList<>();
        new Thread(){

            @Override
            public void run() {
                super.run();
                Connection connection=Jsoup.connect(url);
                connection.timeout(1000);
                try {
                    Document document=connection.get();

                    Element ul=document.select("ul.picbz").get(1);
                    Elements lis=ul.getElementsByTag("li");
                    for (Element li:lis
                            ) {
                        Element a=li.getElementsByTag("a").first();
                        String title=a.attr("title");
                        String imgUri=a.getElementsByTag("img").attr("src");
                        PhotoList photoList=new PhotoList(imgUri,title);
                        oList.add(photoList);
                    }

                    Message message=handler.obtainMessage();
                    message.what=0x34;
                    message.obj=oList;
                    handler.sendMessage(message);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}

3.实体类(PhotoList.java)----存放爬取的网络图片地址,以及网络图片标题

public class PhotoList {
    private String imgUri;
    private String title;

    public PhotoList() {
    }

    public PhotoList(String imgUri, String title) {
        this.imgUri = imgUri;
        this.title = title;
    }

    public String getImgUri() {
        return imgUri;
    }

    public void setImgUri(String imgUri) {
        this.imgUri = imgUri;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

4.适配器(PhotoListAdapter.java)以及子布局(demo_item)

public class PhotoListAdapter extends RecyclerView.Adapter<PhotoListAdapter.ViewHolder> {
    private Context context;
    private List<PhotoList> oList;
    private LayoutInflater inflater;

    public PhotoListAdapter(Context context, List<PhotoList> oList) {
        this.context = context;
        this.oList = oList;
        this.inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate
                (R.layout.demo_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        holder.demo_tv.setText(oList.get(position).getTitle());
        Picasso.with(context).load(oList.get(position).getImgUri()).placeholder(R.mipmap.ic_launcher).into(holder.demo_iv);



    }

    @Override
    public int getItemCount() {
        return oList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView demo_tv;
        private ImageView  demo_iv;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            demo_tv= itemView.findViewById(R.id.demo_tv);
            demo_iv = itemView.findViewById(R.id.demo_iv);

        }
    }


}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:layout_width="150dp"
    android:background="#fcfafa"
    android:layout_height="100dp"
    >
<ImageView
    android:layout_marginTop="6dp"
    android:id="@+id/demo_iv"
    android:layout_width="match_parent"
    android:layout_height="60dp" />
    <TextView
        android:id="@+id/demo_tv"
        android:gravity="center"
        android:textSize="11sp"
        android:layout_width="match_parent"
        android:layout_height="30dp" />
</LinearLayout>

5.爬取的网页源代码的主体部分,查看要爬取的网页源代码就能看到http://www.jj20.com/

<div class="bza2"><script>g("bza2");</script></div>

<div class="g-gxlist-cms g-box-1200 g-main-bg m-margin15 clearfix">
<div class="g-tit-tp"><i></i><em><h1>最新壁纸</h1></em><span><a target="_blank" href="/bz/"></a></span></div>
</div>
<div class="g-box-1200">
<ul class="picbz">
<li><a target="_blank" href="/bz/hhzw/sgsc/334753.html" title="美味的红树莓图片"><img src="http://img.jj20.com/up/allimg/1114/062R1115H6/21062Q15H6-1-lp.jpg" width="298" height="168" alt="美味的红树莓图片"></a><p><a href="/bz/hhzw/sgsc/334753.html">美味的红树莓图片</a>(8张)</p><div class="shadow"></div></li>
<li><a target="_blank" href="/bz/hhzw/yjhx/334752.html" title="郁金香花朵图片大全大图"><img src="http://img.jj20.com/up/allimg/1114/062R1114927/21062Q14927-1-lp.jpg" width="298" height="168" alt="郁金香花朵图片大全大图"></a><p><a href="/bz/hhzw/yjhx/334752.html">郁金香花朵图片大全大图</a>(13张)</p><div class="shadow"></div></li>
<li><a target="_blank" href="/bz/zrfg/smzw/334751.html" title="唯美银杏树图片"><img src="http://img.jj20.com/up/allimg/1114/062R1114F6/21062Q14F6-1-lp.jpg" width="298" height="168" alt="唯美银杏树图片"></a><p><a href="/bz/zrfg/smzw/334751.html">唯美银杏树图片</a>(13张)</p><div class="shadow"></div></li>
<li><a target="_blank" href="/bz/mwjy/mbtd/334750.html" title="高清汉堡包图片大全"><img src="http://img.jj20.com/up/allimg/1114/062R1114120/21062Q14120-1-lp.jpg" width="298" height="168" alt="高清汉堡包图片大全"></a><p><a href="/bz/mwjy/mbtd/334750.html">高清汉堡包图片大全</a>(10张)</p><div class="shadow"></div></li>
<li><a target="_blank" href="/bz/nxxz/shxz/334749.html" title="懵懂少女写真图片"><img src="http://img.jj20.com/up/allimg/1114/062621114A4/210626114A4-1-lp.jpg" width="298" height="168" alt="懵懂少女写真图片"></a><p><a href="/bz/nxxz/shxz/334749.html">懵懂少女写真图片</a>(9张)</p><div class="shadow"></div></li>
<li><a target="_blank" href="/bz/zrfg/fgqt/334748.html" title="高清唯美彩虹图片"><img src="http://img.jj20.com/up/allimg/1114/062621110J7/210626110J7-1-lp.jpg" width="298" height="168" alt="高清唯美彩虹图片"></a><p><a href="/bz/zrfg/fgqt/334748.html">高清唯美彩虹图片</a>(15张)</p><div class="shadow"></div></li>
<li><a target="_blank" href="/bz/zrfg/fgqt/334747.html" title="最美公路风景图片"><img src="http://img.jj20.com/up/allimg/1114/062621104051/210626104051-1-lp.jpg" width="298" height="168" alt="最美公路风景图片"></a><p><a href="/bz/zrfg/fgqt/334747.html">最美公路风景图片</a>(6张)</p><div class="shadow"></div></li>
<li><a target="_blank" href="/bz/zrfg/xsxj/334746.html" title="壮观的雪山图片"><img src="http://img.jj20.com/up/allimg/1114/062621103415/210626103415-1-lp.jpg" width="298" height="168" alt="壮观的雪山图片"></a><p><a href="/bz/zrfg/xsxj/334746.html">壮观的雪山图片</a>(16张)</p><div class="shadow"></div></li>

</ul></div>

<div class="g-gxlist-cms g-box-1200 g-main-bg m-margin15 clearfix">
<div class="g-tit-tp"><i></i><em><h1>风景图片</h1></em><span><a target="_blank" href="/bz/zrfg/"></a></span></div>
</div>
<div class="g-box-1200">
<ul class="picbz">
<li><a target="_blank" href="/bz/zrfg/smzw/334751.html" title="唯美银杏树图片"><img src="http://img.jj20.com/up/allimg/1114/062R1114F6/21062Q14F6-1-lp.jpg" width="298" height="168" alt="唯美银杏树图片"></a><p><a href="/bz/zrfg/smzw/334751.html">唯美银杏树图片</a>(13张)</p><div class="shadow"></div></li>
<li><a target="_blank" href="/bz/zrfg/fgqt/334748.html" title="高清唯美彩虹图片"><img src="http://img.jj20.com/up/allimg/1114/062621110J7/210626110J7-1-lp.jpg" width="298" height="168" alt="高清唯美彩虹图片"></a><p><a href="/bz/zrfg/fgqt/334748.html">高清唯美彩虹图片</a>(15张)</p><div class="shadow"></div></li>
<li><a target="_blank" href="/bz/zrfg/fgqt/334747.html" title="最美公路风景图片"><img src="http://img.jj20.com/up/allimg/1114/062621104051/210626104051-1-lp.jpg" width="298" height="168" alt="最美公路风景图片"></a><p><a href="/bz/zrfg/fgqt/334747.html">最美公路风景图片</a>(6张)</p><div class="shadow"></div></li>
<li><a target="_blank" href="/bz/zrfg/xsxj/334746.html" title="壮观的雪山图片"><img src="http://img.jj20.com/up/allimg/1114/062621103415/210626103415-1-lp.jpg" width="298" height="168" alt="壮观的雪山图片"></a><p><a href="/bz/zrfg/xsxj/334746.html">壮观的雪山图片</a>(16张)</p><div class="shadow"></div></li>
<li><a target="_blank" href="/bz/zrfg/xsxj/334744.html" title="冬天的雪风景图片"><img src="http://img.jj20.com/up/allimg/1114/062421125917/210624125917-1-lp.jpg" width="298" height="168" alt="冬天的雪风景图片"></a><p><a href="/bz/zrfg/xsxj/334744.html">冬天的雪风景图片</a>(14张)</p><div class="shadow"></div></li>
<li><a target="_blank" href="/bz/zrfg/xsxj/334735.html" title="高清冰雪风景图片"><img src="http://img.jj20.com/up/allimg/1114/062021132315/210620132315-1-lp.jpg" width="298" height="168" alt="高清冰雪风景图片"></a><p><a href="/bz/zrfg/xsxj/334735.html">高清冰雪风景图片</a>(8张)</p><div class="shadow"></div></li>
<li><a target="_blank" href="/bz/zrfg/fgqt/334729.html" title="蜿蜒曲折的公路图片"><img src="http://img.jj20.com/up/allimg/1114/061621110643/210616110643-1-lp.jpg" width="298" height="168" alt="蜿蜒曲折的公路图片"></a><p><a href="/bz/zrfg/fgqt/334729.html">蜿蜒曲折的公路图片</a>(12张)</p><div class="shadow"></div></li>
<li><a target="_blank" href="/bz/zrfg/fgqt/334727.html" title="公园里的长椅图片"><img src="http://img.jj20.com/up/allimg/1114/061621110048/210616110048-1-lp.jpg" width="298" height="168" alt="公园里的长椅图片"></a><p><a href="/bz/zrfg/fgqt/334727.html">公园里的长椅图片</a>(11张)</p><div class="shadow"></div></li>

</ul></div>

6.Activity主代码(DemoActivity.java)

public class DemoActivity extends AppCompatActivity {
    private RecyclerView rv_demo1,rv_demo2;
    private PhotoListAdapter photoListAdapter;
    private List<PhotoList> oList1 = new ArrayList<>();
    private List<PhotoList> oList2 = new ArrayList<>();
     private String weburl="http://www.jj20.com/";
     private LinearLayoutManager manager;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        rv_demo1=findViewById(R.id.rv_demo1);
        rv_demo2=findViewById(R.id.rv_demo2);
        JsoupUtil.getInstance().getPhotoLists1(handler,weburl);
        JsoupUtil.getInstance().getPhotoLists2(handler,weburl);

    }
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            manager = new LinearLayoutManager(DemoActivity.this);
            switch (msg.what) {

                case 0x33:
                    oList1 = (List<PhotoList>) msg.obj;
                    photoListAdapter=new PhotoListAdapter(DemoActivity.this,oList1);

                   rv_demo1.setLayoutManager(manager);
                    rv_demo1.setLayoutManager(new StaggeredGridLayoutManager
                            (4, StaggeredGridLayoutManager.VERTICAL));
                    rv_demo1.setAdapter(photoListAdapter);
                    break;
                case 0x34:
                    oList2 = (List<PhotoList>) msg.obj;
                    photoListAdapter=new PhotoListAdapter(DemoActivity.this,oList2);
                    manager = new LinearLayoutManager(DemoActivity.this);
                    rv_demo2.setLayoutManager(manager);
                    rv_demo2.setLayoutManager(new StaggeredGridLayoutManager
                            (4, StaggeredGridLayoutManager.VERTICAL));
                    rv_demo2.setAdapter(photoListAdapter);
                    break;
            }
        }
    };
    }

四、效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值