爬虫

作者: 风的低语 | 来源:发表于2018-07-07 01:49 被阅读15次
爬虫

方法一

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Test {

    public static void main(String[] args) throws IOException {
        Document document = Jsoup.connect("https://www.doutula.com/article/list/?page=8").get();
        //获取图片链接 逗号是同一级元素
        Elements els = document.select(".lazy,.image_dtb,.img-responsive");//css选择器 id class tagName
        
        Iterator<Element> iter = els.iterator();
        while (iter.hasNext()) {
            Element object = (Element) iter.next();
            String imgUrl = object.attr("data-original");
            //获取图片
            if (!imgUrl.contains("http")) {
                continue;
            }
            URL url = new URL(imgUrl);
            URLConnection con = url.openConnection();
            con.connect();
            InputStream stream = con.getInputStream();
            byte[] by = new byte[1024];
            int length = -1;
            //out的流
            String[] imageUrlArr = imgUrl.split("/");
            String fileName = "img/" + imageUrlArr[imageUrlArr.length - 1];
            FileOutputStream fileOutputStream = new FileOutputStream(fileName);
            while ((length = stream.read(by)) != -1) {
                //把流写入到本地文件夹
                fileOutputStream.write(by,0,length);
            }
            fileOutputStream.close();
            stream.close();
        }
    }
}

方法二

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.LinkedList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Test02 {

    public static void main(String[] args) {
        // 创建图片下载对象
        final ImgDownload imgDownload = new ImgDownload();
        // 生产图片链接的
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 1; i < 500; i++) {
                    try {
                        imgDownload.parseImageUrl(i);
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 500; i < 1000; i++) {
                    try {
                        imgDownload.parseImageUrl(i);
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 1000; i < 1500; i++) {
                    try {
                        imgDownload.parseImageUrl(i);
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }
            }
        }).start();

        // 下载
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (true) {
                        imgDownload.downloadImage();
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (true) {
                        imgDownload.downloadImage();
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (true) {
                        imgDownload.downloadImage();
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (true) {
                        imgDownload.downloadImage();
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (true) {
                        imgDownload.downloadImage();
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

class ImgDownload {
    // imgUrl的数组
    LinkedList<String> list = new LinkedList<>();

    // 解析链接
    void parseImageUrl(int page) throws Exception {
        synchronized (this) {
            if (list.size() > 100) {
                wait();
            }
            String htmlUrl = "https://www.doutula.com/article/list/?page=" + page;
            Document document = Jsoup.connect(htmlUrl).get();
            // 获取图片链接 逗号是同一级元素
            Elements els = document.select(".lazy,.image_dtb,.img-responsive");// css选择器
                                                                                // id
                                                                                // class
                                                                                // tagName

            Iterator<Element> iter = els.iterator();
            while (iter.hasNext()) {
                Element object = (Element) iter.next();
                String imgUrl = object.attr("data-original");
                if (!imgUrl.contains("http")) {//如果不包含,则跳过
                    continue;
                }
                this.list.add(imgUrl);
            }
            // 通知可以下载图片
            notifyAll();
        }
    }

    // 下载图片
    void downloadImage() throws Exception {
        synchronized (this) {
            if (this.list.size() <= 1) {
                wait();
            }
//          String imgUrl = null;
//          if (this.list.size() > 0) {
//              imgUrl = this.list.removeFirst();
//          } else {
//              return;
//          }
            String imgUrl = this.list.removeFirst();
            URL url = new URL(imgUrl);
            URLConnection con = url.openConnection();
            con.connect();
            InputStream stream = con.getInputStream();
            byte[] by = new byte[1024];
            int length = -1;
            // out的流
            String[] imageUrlArr = imgUrl.split("/");
            String fileName = "img/" + imageUrlArr[imageUrlArr.length - 1];
            FileOutputStream fileOutputStream = new FileOutputStream(fileName);
            while ((length = stream.read(by)) != -1) {
                // 把流写入到本地文件夹
                fileOutputStream.write(by, 0, length);
            }
            fileOutputStream.close();
            stream.close();
            
            if (this.list.size() < 100) {
                notify();
                //notifyAll();
            }
        }
    }
}

相关文章

  • 11.20-11.26

    本周目标 爬虫 爬虫 爬虫 爬虫

  • 爬虫入门基础

    Day01 一、爬虫介绍 什么是爬虫 Python爬虫的优势 Python爬虫需要掌握什么 爬虫与反爬虫与反反爬虫...

  • 01-认识爬虫

    一、爬虫介绍 什么是爬虫 Python爬虫的优势 Python爬虫需要掌握什么 爬虫与反爬虫与反反爬虫三角之争 网...

  • 爬虫原理与数据抓取之一: 通用爬虫和聚焦爬虫

    通用爬虫和聚焦爬虫 根据使用场景,网络爬虫可分为 通用爬虫 和 聚焦爬虫 两种. 通用爬虫 通用网络爬虫 是 捜索...

  • (了解)通用爬虫和聚焦爬虫--爬虫基础教程(python)(二)

    通用爬虫和聚焦爬虫 根据使用场景,网络爬虫可分为 通用爬虫 和 聚焦爬虫 两种.我们主要写通用爬虫。 通用爬虫 通...

  • Python 网络爬虫(一)

    网络爬虫的基本介绍 学习爬虫,我想主要从以下几个方面来切入 -爬虫的原理? -爬虫的作用? -爬虫的实现? -爬虫...

  • 7.爬虫概述

    爬虫概述 知识点: 了解 爬虫的概念 了解 爬虫的作用 了解 爬虫的分类 掌握 爬虫的流程 1. 爬虫的概念 模拟...

  • 1-基本概念

    简介 为什么选择Python做爬虫 需要技能 爬虫与反爬虫 网络爬虫类型 通用网络爬虫 聚焦网络爬虫 增量式网络爬...

  • 认识爬虫

    前言 我的爬虫笔记 经常看别人通过爬虫分析数据,很有意思,来了兴趣,就开始了爬虫之路。 爬虫 爬虫,即网络爬虫,大...

  • 爬虫入门

    为什么要学习爬虫? Python做爬虫优势 关于Python网络爬虫,我们需要学习的有: 什么是爬虫? 网络爬虫(...

网友评论

      本文标题:爬虫

      本文链接:https://www.haomeiwen.com/subject/voftextx.html