jsoup抓取豆瓣美女

package com.huowolf;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

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

public class Demo4 {
	
	private static final String url = "http://www.dbmeinv.com/?p=";
	private static final String picPath = "d:/picTest";
	private static String USER_AGENT="Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0";
	
	public static void main(String[] args) {
		System.out.println("下载完的图片位于d:/picTest");
		for (int i = 0; i < 50; i++) {
				try {
				Document doc = Jsoup.connect(url+i).userAgent(USER_AGENT).timeout(3000).data("pager_offset", i+1+"").post();
				Elements img = doc.select("img");
				for (Element ele : img) {
					String src = ele.absUrl("src");
					//System.out.println(src);
					getImage(src);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		System.out.println("这些足够多了!");
		System.out.println("图片下载完成!");
	}
	
	
	private static void getImage(String src) {
		int indexName = src.lastIndexOf("/");
		String name = src.substring(indexName, src.length());
		//System.out.println(name);
		InputStream in = null;  
		OutputStream out = null;
		try {
			URL url = new URL(src);
			in = url.openStream();
			
			//创建文件夹
			File files = new File(picPath);
			if(!files.exists())
				files.mkdirs();
			
			out = new BufferedOutputStream(new FileOutputStream(files+name));
			for(int b;(b=in.read())!=-1;)
				out.write(b);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				out.close();
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		
	}
}

程序已打包,拿去双击即可运行,下载地址:http://pan.baidu.com/s/1mgKiQiG

运行结果图:


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值