爬取 wallhaven图片到本地壁纸库
项目地址,另外知乎同名文章也是我发布的,大家可以多多关注
首先观察控制台
其次再看本地壁纸库
现在进入正题,这个小项目用到了 Jsoup
具体版本见 POM
),另外还用到了 JDK
中的线程池、阻塞队列(生产-消费者模式)、NIO2
(文件监听服务 API
),所以至少要求 JDK
版本为7或者以上
项目分为5个类和一个方法入口类
生产者类(任务:从列表页拿到详情页链接并放入阻塞队列)
public class Producer implements Runnable {private String name;private BlockingQueue<String> blockingQueue;public Producer(String name, BlockingQueue<String> blockingQueue) {this.name = name;this.blockingQueue = blockingQueue;}@Overridepublic void run() {Document doc = null;try {for(int i = 1; i < 12018; i ++) {System.out.println();System.out.println();System.out.println("current page:" + i);System.out.println("-----------------------------------");if(i == 1) {doc = Jsoup.connect("https://alpha.wallhaven.cc/latest").get();} else {doc = Jsoup.connect("https://alpha.wallhaven.cc/latest?page=" + i).get();}Element div = doc.getElementById("thumbs");Elements sections = div.getElementsByTag("section");for (Element ele : sections) {Elements links = ele.getElementsByClass("preview");for (Element e : links) {String href = e.attr("href");blockingQueue.put(href);System.out.println(name + " put " + href);}}}blockingQueue.put("");System.out.println(name + " is over");} catch (IOException | InterruptedException e) {e.printStackTrace();} }
}
消费者类(任务:从队列拿到链接并获取图片源地址并将下载任务交给一个缓存线程池)
public class Consumer implements Runnable {private String name;private BlockingQueue<String> blockingQueue;private ExecutorService taskPool;public Consumer(String name, BlockingQueue<String> blockingQueue, ExecutorService taskPool) {this.name = name;this.blockingQueue = blockingQueue;this.taskPool = taskPool;}@Overridepublic void run() {Document doc = null;try {String href = null;while((href = blockingQueue.take()) != "") {System.out.println(name + " take " + href);doc = Jsoup.connect(href).get();Element img = doc.getElementById("wallpaper");String src = "https:" + img.attr("src");taskPool.submit(new DownloadTask(src));}System.out.println(name + " is over");} catch (IOException | InterruptedException e) {e.printStackTrace();} }}
下载任务执行类(任务:下载图片到本地)
public class DownloadTask implements Runnable {private static String path = "C:\\Users\\baiyapeng\\Desktop\\Paper\\";private String src;private String name;public DownloadTask(String src) {this.src = src;int n = src.lastIndexOf("/");this.name = src.substring(++n);}@Overridepublic void run() {Response res = null;try {res = Jsoup.connect(src).ignoreContentType(true).timeout(30000).execute();byte[] bytes = res.bodyAsBytes();File file = new File(path + name);if (!file.exists()) {RandomAccessFile raf = new RandomAccessFile(file, "rw");raf.write(bytes);raf.close();}} catch (IOException e1) {e1.printStackTrace();}}}
监听服务类(任务:将文件路径注册到监听服务上并开始监听)
public class ResourceListener {private static ExecutorService fixedThreadPool = Executors.newCachedThreadPool();private WatchService ws;private ResourceListener(String path) {try {ws = FileSystems.getDefault().newWatchService();start();} catch (IOException e) {e.printStackTrace();}}private void start() {fixedThreadPool.execute(new Listener(ws));}public static void addListener(String path) {try {ResourceListener resourceListener = new ResourceListener(path);Path p = Paths.get(path);p.register(resourceListener.ws, StandardWatchEventKinds.ENTRY_CREATE);} catch (IOException e) {e.printStackTrace();}}}
监听回调类(任务:执行回调任务)
public class Listener implements Runnable {private WatchService service;public Listener(WatchService service) {this.service = service;}@Overridepublic void run() {try {while (true) {WatchKey watchKey = service.take();List<WatchEvent<?>> watchEvents = watchKey.pollEvents();for (WatchEvent<?> event : watchEvents) {System.err.println(event.context() + "已下载");}watchKey.reset();}} catch (InterruptedException e) {e.printStackTrace();} }
}
方法入口类
public class DownloadTaskExecutor {public static void main(String[] args) throws IOException {ResourceListener.addListener("C:\\Users\\baiyapeng\\Desktop\\Paper\\");BlockingQueue<String> blockingQueue = new SynchronousQueue<String>(true);ExecutorService proservice = Executors.newSingleThreadExecutor();ExecutorService conservice = Executors.newSingleThreadExecutor();ExecutorService taskPool = Executors.newCachedThreadPool();proservice.submit(new Producer("Producer", blockingQueue));conservice.submit(new Consumer("Consumer", blockingQueue, taskPool));proservice.shutdown();conservice.shutdown();}}
最后就是设置壁纸库并设定更换频率
感谢大家,有问题可以再评论区留言~~
爬取 wallhaven图片到本地壁纸库相关推荐
- Python爬取网页图片至本地
Python爬取网页图片至本地 爬取网页上的图片至本地 参考代码如下: # -*- codeing = utf-8 -*- import requests import rephotos = [] h ...
- 爬取美女图片保存本地与入MySQL库(宅男福利)
本文详细记录如何爬取美女图片,并将图片下载保存在本地,同时将图片url进行入库.保存在本地肯定是为了没事能拿出来养养眼啊,那入库就是为了定位图片啊,要懂点技术的话,还能搬运搬运做个小图片网站,不为别的 ...
- 用 Jupyter Notebook 爬取微博图片保存本地!
今天咱们用 Jupyter-Notebook 并结合框架(Selenium)模拟浏览器抓取微博图片并将图片保存本地. Selenium 是一个用电脑模拟人的操作浏览器网页,可以实现自动化测试,模拟浏览 ...
- 爬虫实战-爬取wallhaven图片(小白入门)
(最近学业课程较为紧张,码代码的时间比较紧,利用零散时间终于做完了此次爬取) 作为爬虫小白,代码偏向简单,大佬勿喷~ 本次爬取网站:https://wallhaven.cc/toplist 本次所用工 ...
- [Python3] 爬取百度图片到本地
前言 因为需要一些图片素材,又不想一个个手动下载,遂通过爬虫来解放双手.在百度图片中搜索"汉服美女",然后以浏览器地址栏上的地址作为初始 URL.通过对 URL 分析知道 URL ...
- Java爬虫爬取wallhaven的图片
Java爬虫爬取wallhaven的图片 参考文章:JAVA Jsoup爬取网页图片下载到本地 需要的jar包:jsuop wallhaven网站拒绝java程序访问,所以要伪装报头. 发送请求时 C ...
- json返回的img图片被原样输出_爬取百度图片,并下载至本地
爬取百度图片 一:本节目标 本次爬取的目标是百度图片,将图片下载到本地 二:准备工作 安装Scrapy.Python3 三:爬取思路 我们需要实现的是下载图片,所以需要获取图片的真实链接 四:爬取分析 ...
- 小爬虫爬取小猫咪图片并存入本地文件夹
小爬虫爬取小猫咪图片并存入本地文件夹 本人是安徽工业大学电气与信息工程学院研一学生,最近还不能开学真的是很糟心哦,由于自己比较笨吧,起步较晚还要忙着学习机器学习还有计算机视觉,但是总学这个感觉很闷也没 ...
- node.js 爬虫 实现爬取网页图片并保存到本地
node.js 爬虫 实现爬取网页图片并保存到本地 没有废话直接看代码 /*** 请求网站数据* 将数据保存本地文件*/ //不同协议引用不同模块,http https const http = re ...
最新文章
- ASP.NET 学习历程
- 为数字资产交易设计安全的钱包架构
- 双 11 的狂欢,干了这碗「流量防控」汤
- ICSharpCode.SharpZipLib 压缩
- ubuntu终端快捷键
- C# 框架是什么?MVC是什么 ?工厂模式是什么?设计模式是什么?三层架构是什...
- Floodlight 在 ChannelPipeline 图
- matplotlib plot 绘图函数发生阻塞(block)时的解决方法
- scala案例_Scala案例类和案例对象深入(第2部分)
- 【渝粤教育】电大中专药剂学基础知识 (2)_1作业 题库
- 关于treeview中的checkbox的全选问题
- 关于QT 报错Error: Class declaration lacks Q_OBJECT macro.
- 百度BML飞桨训练营(五)商品种类识别
- 可口可乐迎来重大人事变动:全球COO、CFO和CTO都要换人
- matlab曲线加颜色,matlab学习之绘制参数曲线,添加辅助线以及颜色设置
- 7 款 DevOps 工具管理 Kubernetes
- 中国人太空行走的一天
- 微信小程序(游戏)----拖拽拼图(图片分块和打乱顺序)
- python 决策模型_【Spark MLlib速成宝典】模型篇05决策树【Decision Tree】(Python版)...
- soft-attention (SENet、BAM、CBAM)