先看看抓取的结果。
8个Java类:
- Startup.java - main函数
- ImageCrawler.java - Crawler基类
- BaiduImageCrawler.java - 百度图片的具体爬取实现
- GoogleImageCrawler.java - Google图片的具体爬取实现
- BingImageCrawler.java - Bing图片的具体爬取实现
- ImageWorker.java - 定时从Queue中取图片URL下载(默认开启100个)
- ImageDownloader.java - 图片下载
- MD5Checksum.java - 文件MD5计算(文件去重)
- public class BaiduImageCrawler extends ImageCrawler {
- //tn:resultjsonavatarnew
- //ie:utf-8 字符编码(ie输入 oe输出)
- //word:美女 搜索关键字
- //pn:60 开始条数
- //rn:30 显示数量
- //z:0 尺寸(0全部尺寸 9特大 3大 2中 1小)
- //width:1024 自定义尺寸-宽
- //height:768 自定义尺寸-高
- //ic:0 颜色(0全部颜色 1红色 2黄色 4绿色 8青色 16蓝色 32紫色 64粉色 128棕色 256橙色 512黑色 1024白色 2048黑白)
- //s:0 3头像图片
- //face:0 1面部特写
- //st:-1 -1全部类型 1卡通画 2简笔画
- //lm:-1 (6动态图片 7静态图片)
- //gsm:3c pn值的十六进制数
- private static final String BAIDU_IMAGE_SEARCH_URL = "http://image.baidu.com/search/avatarjson?tn=resultjsonavatarnew&ie=utf-8&word=%s&pn=%d&rn=%d&z=3&ic=0&s=0&face=0&st=-1&lm=-1&gsm=%s";
- private static final int PAGE_SIZE = 60;
- private static final String IMAGE_URL_REG = "\"objURL\":\"(https?://[^\"]+)\"";
- private static final Pattern IMAGE_PATTERN = Pattern.compile(IMAGE_URL_REG);
- @Override
- public String getSearchUrl(String keyword, int page) {
- int begin = page * PAGE_SIZE;
- return String.format(BAIDU_IMAGE_SEARCH_URL, keyword, begin, PAGE_SIZE, Integer.toHexString(begin));
- }
- @Override
- public int parseImageUrl(ConcurrentLinkedQueue<String> queue, StringBuffer data) {
- int count = 0;
- Matcher matcher = IMAGE_PATTERN.matcher(data);
- while (matcher.find()) {
- queue.offer(matcher.group(1));
- count++;
- }
- return count;
- }
- }
- public class GoogleImageCrawler extends ImageCrawler {
- //tbm=isch
- //q=美女 搜索关键字
- //ijn=0 页码(***Google只提供到0到8页数据!)
- //start=0 开始条数
- //tbs=isz:l 搜索条件
- // 尺寸
- // tbs=isz:l 大
- // tbs=isz:m 中
- // 颜色
- // tbs=ic:color 彩色
- // tbs=ic:gray 黑白
- // tbs=ic:trans 透明
- // 类型
- // tbs=itp:face 脸部特写
- // tbs=itp:photo 照片
- // tbs=itp:clipart 剪贴画
- // tbs=itp:lineart 素描画
- // tbs=itp:animated 动画
- // 条件组合
- // tbs=isz:l,ic:color,itp:face
- private static final String GOOGLE_IMAGE_SEARCH_URL = "https://www.google.com/search?tbm=isch&q=%s&ijn=%d&start=%d&tbs=isz:l";
- private static final int PAGE_SIZE = 100;
- private static final String IMAGE_URL_REG = "\"ou\":\"(https?://[^\"]+)\"";
- private static final Pattern IMAGE_PATTERN = Pattern.compile(IMAGE_URL_REG);
- @Override
- public String getSearchUrl(String keyword, int page) {
- int begin = page * PAGE_SIZE;
- return String.format(GOOGLE_IMAGE_SEARCH_URL, keyword, page, begin);
- }
- @Override
- public int parseImageUrl(ConcurrentLinkedQueue<String> queue, StringBuffer data) {
- int count = 0;
- Matcher matcher = IMAGE_PATTERN.matcher(data);
- while (matcher.find()) {
- queue.offer(matcher.group(1));
- count++;
- }
- return count;
- }
- }
- public class BingImageCrawler extends ImageCrawler {
- //async=content
- //q=美女 搜索关键字
- //first=118 开始条数
- //count=35 显示数量
- private static final String BING_IMAGE_SEARCH_URL = "http://www.bing.com/images/async?async=content&q=%s&first=%d&count=%d";
- private static final int PAGE_SIZE = 35;
- private static final String IMAGE_URL_REG = "imgurl:"(https?://[^,]+)"";
- private static final Pattern IMAGE_PATTERN = Pattern.compile(IMAGE_URL_REG);
- @Override
- public String getSearchUrl(String keyword, int page) {
- int begin = page * PAGE_SIZE;
- return String.format(BING_IMAGE_SEARCH_URL, keyword, begin, PAGE_SIZE);
- }
- @Override
- public int parseImageUrl(ConcurrentLinkedQueue<String> queue, StringBuffer data) {
- int count = 0;
- Matcher matcher = IMAGE_PATTERN.matcher(data);
- while (matcher.find()) {
- queue.offer(matcher.group(1));
- count++;
- }
- return count;
- }
- }