用java写的一个图片爬虫脚本_代码分享
来源:互联网 发布:淘宝凯洛伦面具 编辑:程序博客网 时间:2024/06/03 15:59
这篇文章主要介绍了,如何用纯java写一个图片爬虫,美女图片爬虫代码分享,本文以采集抓取美女图片为例,需要的朋友可以参考下继续鼓捣爬虫,你懂的!
咳咳!说的我都有点兴奋了,容我静静。
1、需要用到的架包,以下就是(需要架包的朋友,可以私聊我。):
2.程序入口(取个好名叫index)
import java.io.InputStream;import org.apache.http.client.config.CookieSpecs;import org.apache.http.client.config.RequestConfig;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;public class index {private static final int page = 1264;public static void main(String[] args) {//HttpClient 超时配置RequestConfig Config = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).setConnectionRequestTimeout(6000).setConnectTimeout(6000).build();CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(Config).build();System.out.println("开始lol....");for (int i = page; i > 0; i--) {HttpPost httpPost = new HttpPost("http://www.jf258.com/nansheng/"+ i+"1.html"); //需要爬的网站httpPost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");//伪装一个浏览器try {CloseableHttpResponse response = httpClient.execute(httpPost);//开始InputStream ism = response.getEntity().getContent();String context = Utils.convertStreamToString(ism);new Thread(new CheDHtmlParser(context, i)).start();} catch (Exception e) {e.printStackTrace();}}}}</strong></span>2、请求页面分析
import java.util.List;public class CheDHtmlParser implements Runnable {private String html;private int page;public CheDHtmlParser(String html,int page) {this.html = html;this.page = page;}@Overridepublic void run() {List<String> list = new ArrayList<String>();html = html.substring(html.indexOf("list"));String[] ss = html.split("li>");for (String s : ss) {if (s.indexOf("<img src=") > 0) {try{int i = s.indexOf("<img src=\"") + "<img src=\"".length();list.add(s.substring(i, s.indexOf("\"", i + 1)));}catch (Exception e) {System.out.println(s);}}}for(String imageUrl : list){new Thread(new CheDImageCreator(imageUrl,page)).start();}}}</strong></span>3、文件流部分(主要是将分析好的请求页面,做一个字符转换,方便后面截取图片的路径)
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;public class Utils {public static String convertStreamToString(InputStream in) {BufferedReader reader = new BufferedReader(new InputStreamReader(in));StringBuilder sb = new StringBuilder();String line = null;try {while ((line = reader.readLine()) != null) {sb.append(line + "/n");}} catch (IOException e) {e.printStackTrace();} finally {try {in.close();} catch (IOException e) {e.printStackTrace();}}return sb.toString();}}</strong></span>4、图片创建
import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.net.URL;import java.net.URLConnection;public class CheDImageCreator implements Runnable {private static int count = 0;private String imageUrl;private int page; //存储路径private static final String basePath = "D:/tupian"; public CheDImageCreator(String imageUrl,int page) {this.imageUrl = imageUrl;this.page = page;}@Overridepublic void run() {File dir = new File(basePath);if(!dir.exists()){dir.mkdirs();}String imageName = imageUrl.substring(imageUrl.lastIndexOf("/")+1);//获取图片名字try {File file = new File( basePath+"/"+page+"--"+imageName);//拼接OutputStream os = new FileOutputStream(file); //创建一个url对象 String u="http://www.jf258.com"+imageUrl; URL uri = new URL(u); URLConnection connection = uri.openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");//伪装成一个浏览器 InputStream is = connection.getInputStream(); //开始一个流byte[] buff = new byte[1024];while(true) {int readed = is.read(buff);if(readed == -1) {break;}byte[] temp = new byte[readed];System.arraycopy(buff, 0, temp, 0, readed);//写入文件os.write(temp);}System.out.println("第"+(count++)+"张:"+file.getAbsolutePath());is.close(); os.close();} catch (Exception e) {e.printStackTrace();}}}</strong></span>5、最后的效果就是这样的了 ,如图:
1 0
- 用java写的一个图片爬虫脚本_代码分享
- 分享一个好玩的python爬虫代码
- 分享一个java写的中国象棋代码以及相关视频
- 用python写一个美女图片爬虫
- java写一个爬虫
- 用java写了一个非常简单的网页爬虫
- 用shell写了一个自动编译代码的脚本
- 用shell写了一个自动编译代码的脚本
- 分享一个java写的简单计算器
- 一个用shell写的执行java程序的脚本
- 分享一个自己写的用python比对数据库表数据的脚本
- 用perl写的一个网络爬虫
- 用Python写一个简单的爬虫
- Java写的爬虫
- java写的爬虫
- 分享一个单机游戏的NSIS封装包脚本代码
- java写的一个简单的爬虫(jsoup)
- shell脚本写的一个图片抓取器
- 【HDU 5653】Bomber Man wants to bomb an Array.|DP
- 定时任务:Handler vs Timer
- 探秘Java虚拟机——内存管理与垃圾回收
- Deep Learning in NLP (一)词向量和语言模型
- odoo8中在“更多”下拉菜单下实现简单的批量操作
- 用java写的一个图片爬虫脚本_代码分享
- Android应用开发:图形绘制之硬件加速
- Activity设置透明背景
- html的form中动态添加action
- HttpURLConnection中econnreset异常之后的理解
- 【poj 1942】Paths on a Grid 题意&题解&代码(C++)
- 在使用Poco库调试时出现异常,异常位置:EnterCriticalSection(&_cs);
- hadoop2.6伪分布式配置
- 计算机网络应用层和传输层及网络层协议有哪些?