Java jsoup多线程爬虫(爬豆瓣图书封面)
来源:互联网 发布:phpcms源码 编辑:程序博客网 时间:2024/05/16 05:05
Java的一个爬虫,就先爬个好爬的豆瓣读书的封面。
用的是第三方库jsoup,也有其他的比如WebMagic之类的
利用线程池多线程爬,biubiubiu,速度超快。
下载到指定的文件夹中。
App.java:
package com.zmz.spider;import java.io.IOException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.nodes.Element;import org.jsoup.select.Elements;public class App { public static void main(String[] args) { // 确定目标地址 URL 统一资源定位符 String url="https://book.douban.com/"; // 2 解析 html : https://jsoup.org try { // Document doc = Jsoup.connect(url).get(); // System.out.println(doc.title());// System.out.println(doc.html()); //从 Doc 的树形结构中查找 img 标签 //.class 选择器 Elements els = doc.select(".cover img"); System.out.println(els.size()); // 创建一个线程池 //.class 选择器 ExecutorService pool = Executors.newCachedThreadPool(); pool = Executors.newFixedThreadPool(9);// pool = Executors.newSingleThreadExecutor(); for(Element e : els) { // <img src="" width="" height="" /> String src = e.attr("src"); System.out.println(src); // 下载每张图片 pool.execute(new DownloadTask(src)); } //释放资源 pool.shutdown(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
package com.zmz.spider;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import javax.net.ssl.HttpsURLConnection;/** * 负责下载图片的任务,可以由线程执行 (Runnable) * * @author zmz * */public class DownloadTask implements Runnable { //图片的路径 String imagePath; /** * * @param src * 图片的位置和路径 */ public DownloadTask(String src) { imagePath = src; } @Override public void run() { //建立一个HTTP连接,使用输入流获得数据,使用输出流写入磁盘 HttpURLConnection conn = null; InputStream in = null; FileOutputStream out = null; try { conn = (HttpURLConnection) new URL(imagePath).openConnection(); //读取数据 in = conn.getInputStream(); String uu = "G:\\Newer_Project\\Spider\\img\\"; //获得图片的名字 int index = imagePath.lastIndexOf('/'); String file = imagePath.substring(index + 1); file = uu + file; //创建输出流,写入 out = new FileOutputStream(file); byte[] buf = new byte[1024 + 16]; int size; while(-1 != (size = in.read(buf))) { out.write(buf, 0, size); } //下载完成 String name = Thread.currentThread().getName(); System.out.println(name + "下载" + imagePath); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { //不论是否发生异常都会执行的 if(out != null) { try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(conn != null) { conn.disconnect(); } } }}
后续是不是可以翻页爬的,因为这个只是爬当前页面的,豆瓣读书网的书还有很多页,我们爬完这一页的,继续爬下一页?
阅读全文
0 0
- Java jsoup多线程爬虫(爬豆瓣图书封面)
- Python爬虫爬取豆瓣图书的信息和封面,放入MySQL数据库中。
- <Python爬虫>爬取豆瓣图书/豆瓣电影系列
- 简单的python爬虫爬豆瓣图书TOP250
- Python爬虫入门 | 4 爬取豆瓣TOP250图书信息
- 使用Jsoup对豆瓣读书进行爬虫
- 用Jsoup爬虫抓取豆瓣书籍信息
- python爬虫之豆瓣图书信息几行字
- [Python爬虫]2.豆瓣图书Top250
- 简单多线程爬虫+Jsoup分析
- java jsoup 网络爬虫
- Java爬虫之Jsoup
- java爬虫:JSOUP
- python爬取豆瓣图书
- java爬虫 爬取图书信息
- java jsoup 网络爬虫 学习例子(二) 只抓取豆瓣电影5星(力荐)电影名称
- python爬虫——获取豆瓣评分图书
- [R语言爬虫] 豆瓣网,10月图书抓取
- LinuxStudyNote(19)-Linux常用命令(2)-文件处理命令(14)-文件显示(5)-head显示文件前几行
- Windows下SOLR7.0.0的安装
- STL库中的vector的使用和模拟实现
- spring boot之创建第一个Spring boot项目
- python变量及运算
- Java jsoup多线程爬虫(爬豆瓣图书封面)
- C#学习笔记4-string和StringBuilder
- ios导航栏rightBarButtonItems多个按钮自定义设置
- Spring Data JPA进阶-Specifications和Querydsl
- 非监督特征学习与深度学习(五)----Softmax 回归(Softmax Regression)
- Java的String、StringBuilder以及StringBuilder和StringBuffer的区别
- MySQL——不含公共字段和id的多个表的一对一关联查询
- ArrayList源码分析
- vim编辑文件去除^M及解决办法