抓取QQ空间 相册图片

package com.sxit;

import java.io.BufferedInputStream;
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.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.GzipDecompressingEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 * @功能:QQ空间相册批量下载(公开相册)
 * @作者: smile
 * @时间:2013-2-5 下午6:08:56
 * @版本:1.0
 */
public class QQPic {

	private static HttpClient client = new DefaultHttpClient();
	// 存放图片信息Bean
	private static List<QQBean> pList = new ArrayList<QQBean>();
	// 服务器前缀
	private static List<String> list = new ArrayList<String>();

	static {
		// 连接超时3秒中 轮询 网速不好的时候这个可以改大点
		client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000);
		// 读取超时
		// client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 3000);

		// 相册默认服务器前缀
		list.add("hzalist");
		// 图片默认服务器前缀
		list.add("hzplist");

		// 下面这些是备用的
		// list.add("hzup");
		// list.add("hz");
		//
		// list.add("gzup");
		// list.add("gz");
		// list.add("gzalist");
		// list.add("gzplist");
		//
		// list.add("up");
		// list.add("alist");
		// list.add("plist");
		// list.add("xaup");
		// list.add("xa");
		//
		// list.add("alistbak");
		// list.add("plistbak");
		// list.add("xaalistbak");
		// list.add("xaplistbak");
	}

	public static void main(String[] args) {

		Random rand = new Random();

		try {
			while (true) {
				long before = System.currentTimeMillis();
				String s = "";
				for (int i = 0; i < 9; i++) {
					s += rand.nextInt(9);
				}
				// 验证是否能够访问
				String msg = sexAnalyse(s);
				if (!msg.equals("xxoo")) {
					System.out.println("开始访问QQ号为:" + s + "的空间");
					// Scanner scan = new Scanner(System.in);
					// String qq = "";
					// System.out.println("请输入你的QQ号:");
					// qq = scan.nextLine();
					// String password = "";
					// System.out.println("请输入你的QQ号密码:");
					// password = scan.nextLine();
					// String target = "";
					// System.out.println("请输入目标的QQ号:");
					// target = scan.nextLine();
					// login(qq, password, target, "男");
					login("*******", "*******", s, msg);
					long after = System.currentTimeMillis();
					System.out.println("QQ号为:" + s + ":总共花费" + (after - before) / 1000 + "s");
				}
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 区分男女 有无权限
	public static String sexAnalyse(String qq) throws ClientProtocolException, IOException {
		HttpGet get = new HttpGet("http://user.qzone.qq.com/" + qq);
		get.setHeader("Accept", "application/javascript, */*;q=0.8");
		get.setHeader("Accept-Encoding", "gzip, deflate");
		get.setHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");

		HttpResponse response = client.execute(get);
		String entity = EntityUtils.toString(new GzipDecompressingEntity(response.getEntity()));
		// System.out.println(entity);
		if (entity.indexOf("她的主页") > 0) {
			System.out.println("QQ号为:" + qq + "是女的空间,访问!");
			return "女";
		} else if (entity.indexOf("温馨提示") > 0) {
			System.out.println("QQ号为:" + qq + "是无效空间,不访问!");
		} else if (entity.indexOf("无权限") > 0) {
			System.out.println("QQ号为:" + qq + "无权限,不访问!");
		} else if (entity.indexOf("他的主页") > 0) {
			System.out.println("QQ号为:" + qq + "是男的空间,访问!");
			return "男";
		}
		return "xxoo";
	}

	// 验证是否存在该文件 不存在则创建
	public static void isUserFile(String sex, String targetname) {

		File f = new File("E:\\tmp\\" + sex + "\\" + targetname);
		if (!f.exists()) {
			f.mkdir();
		}
	}

	// 验证是否存在该文件 不存在则创建
	public static void isAblumFile(String sex, String targetname, String ablumname) {

		File f = new File("E:\\tmp\\" + sex + "\\" + targetname + "\\" + ablumname);
		if (!f.exists()) {
			f.mkdir();
		}
	}

	// 下载
	public static void upload(String targetname) throws Exception {

		InputStream is = null;
		OutputStream os = null;
		URL url = null;
		HttpURLConnection con = null;

		try {
			if (pList != null && pList.size() > 0) {
				System.out.println("开始下载,图片总量为:" + pList.size());
				for (int i = 0; i < pList.size(); i++) {
					try {
						QQBean bean = pList.get(i);

						// 性别
						String sex = bean.getSex();
						// 用户目录
						String targetFile = bean.getTargetName();

						isUserFile(sex, targetFile);

						// 相册目录
						String albumFile = bean.getAlbumName();
						albumFile = albumFile.replace(".", "").replace("\\", "").replace("/", "").replace(",", "").replace(",", "").replace("。", "").replace("<", "").replace(">", "").replace("《", "").replace("》", "").replace("(", "").replace(")", "").replace("(", "").replace(")", "").replace("!", "").replace("@", "").replace("#", "").replace("$", "").replace("%", "").replace("^", "").replace("&", "").replace("&", "").replace("*", "").replace("-", "").replace("——", "").replace("+", "").replace("=", "").replace("[", "").replace("]", "").replace("{", "").replace("}", "").replace(";", "").replace("'", "").replace(":", "").replace("\"", "").replace(" ", "").replace("?", "").replace("?", "");

						isAblumFile(sex, targetFile, albumFile);

						url = new URL(bean.getPicUrl());
						con = (HttpURLConnection) url.openConnection();
						// 设置连接超时
						con.setConnectTimeout(5 * 1000);
						// 设置读取超时
						con.setReadTimeout(10 * 1000);
						is = new BufferedInputStream(con.getInputStream());
						File f = new File("E:\\tmp\\" + sex + "\\" + targetFile + "\\" + albumFile + "\\" + (bean.getIndex() + 1) + ".jpg");
						os = new BufferedOutputStream(new FileOutputStream(f));
						byte[] b = new byte[1024];
						int length = 0;
						while ((length = is.read(b)) != -1) {
							os.write(b, 0, length);
						}
						os.flush();
						if (i != 0 && i % 10 == 0) {
							System.out.print("第" + (i + 1) + "张图\t");
						} else {
							if ((i + 1) % 10 == 0) {
								System.out.print("第" + (i + 1) + "张图\n");
							} else {
								System.out.print("第" + (i + 1) + "张图\t");
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
						continue;
					}
				}
				System.out.println("\n+++++++++++++++图片下载完毕+++++++++++++++");
			} else {
				System.out.println("无相片信息!");
			}
		} finally {
			if (is != null) {
				is.close();
			}
			if (os != null) {
				os.close();
			}
		}
	}

	// 登入空间
	public static void login(String username, String password, String targetname, String sex) throws Exception {

		// 获取验证码
		HttpGet get = new HttpGet("http://check.ptlogin2.qq.com/check?uin=" + username + "&appid=549000913&ptlang=2052&js_type=2&js_ver=10009&r=" + Math.random());
		get.setHeader("Accept", "application/javascript, */*;q=0.8");
		get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 SE 2.X MetaSr 1.0");

		HttpResponse response = client.execute(get);
		String entity = EntityUtils.toString(response.getEntity());
		// System.out.println(entity);

		String[] checkNum = entity.substring(entity.indexOf("(") + 1, entity.lastIndexOf(")")).replace("'", "").split(",");
		// System.out.println(checkNum[0]);
		// System.out.println(checkNum[1]);
		// System.out.println(checkNum[2].trim());
		// System.out.println(checkNum[2].trim().replace("\\x", ""));

		password = QQmd5.GetPassword(checkNum[2].trim(), password, checkNum[1].trim());
		// 加密后的密码
		// System.out.println(password);

		// 登录 用自己的QQ登入扒好友的相冊  跑了一小时就显示网络异常
		get = new HttpGet("http://ptlogin2.qq.com/login?ptlang=2052&u=" + username + "&p=" + password + "&verifycode=" + checkNum[1] + "&aid=549000913&u1=http://qzs.qq.com/qzone/v5/loginsucc.html?para=izone&ptredirect=1&h=1&from_ui=1&dumy=&fp=loginerroralert&action=7-26-87449&g=1&t=2&dummy=&js_type=2&js_ver=10009");
		get.setHeader("Accept", "application/javascript, */*;q=0.8");
		get.setHeader("Accept-Encoding", "gzip, deflate");
		get.setHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");

		response = client.execute(get);
		entity = EntityUtils.toString(response.getEntity());
		// System.out.println(entity);

		// if (entity.indexOf("登录成功") > -1) {
		get = new HttpGet("http://user.qzone.qq.com/" + username);
		get.setHeader("Accept", "application/javascript, */*;q=0.8");
		get.setHeader("Accept-Encoding", "gzip, deflate");
		get.setHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");

		response = client.execute(get);

		Header header = response.getEntity().getContentEncoding();
		// 判断页面是否被压缩过
		// if (header != null) {
		// for (HeaderElement element : header.getElements()) {
		// if (element.getName().equalsIgnoreCase("gzip")) {
		// entity = EntityUtils.toString(new GzipDecompressingEntity(response.getEntity()));
		// break;
		// }
		// }
		// }
		entity = EntityUtils.toString(new GzipDecompressingEntity(response.getEntity()));
		// System.out.println(entity);

		/*********************************************************************************************
		 * 用户已登入 开始访问别人用户相册 50e50923-2f1a-4eda-86d5-7887749dc16d V12CIRQc0rx9Bu http://gzalist.photo.qq.com/fcgi-bin/fcg_list_album_v2? inCharset=gbk&outCharset=gbk&hostUin=909042702&notice=0&callbackFun= &format=jsonp&plat=qzone&source=qzone&appid=4&uin=461504978&t=0.6304595777950794&g_tk=1446409337 http://hzalist.photo.qq.com/fcgi-bin/fcg_list_album_v2?inCharset=gbk&outCharset=gbk&hostUin=315597239&notice=0&callbackFun=&format=jsonp&plat=qzone&source=qzone&appid=4&uin=461504978&t=0.2735186883334789&g_tk=1446409337
		 *********************************************************************************************/

		// 存放可以访问的每个相册名和id
		List<String> ablumStr = new ArrayList<String>();
		for (int i = 0; i < 1; i++) {
			// 轮询前两个前缀直接跳出 默认服务器
			if (i == 2) {
				break;
			}
			// 这里是获取相册id
			get = new HttpGet("http://" + list.get(i) + ".photo.qq.com/fcgi-bin/fcg_list_album_v2?inCharset=gbk&outCharset=gbk&hostUin=" + targetname + "&notice=0&callbackFun=&format=jsonp&plat=qzone&source=qzone&appid=4&uin=" + username + "&t=" + Math.random() + "&g_tk=1446409337");
			get.setHeader("Accept", "application/javascript, */*;q=0.8");
			get.setHeader("Accept-Encoding", "gzip, deflate");
			get.setHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");

			try {
				response = client.execute(get);
				entity = EntityUtils.toString(new GzipDecompressingEntity(response.getEntity()));
				entity = entity.substring(10, entity.length() - 2);
				JSONObject json = new JSONObject(entity);
				json = (JSONObject) json.get("data");
				JSONArray array = json.getJSONArray("album");
				// System.out.println(array.length());
				if (array != null) {
					// System.out.println("****************************************");
					// System.out.println("相册在" + list.get(i) + "前缀服务器上,共有"+array.length()+"个相册");
					for (int j = 0; j < array.length(); j++) {
						JSONObject obj = (JSONObject) array.get(j);
						// 是否能够访问allowAccess 1为可以访问
						String allowAccess = obj.get("allowAccess").toString();
						if (allowAccess.equals("1")) {
							// 相册id
							String id = obj.get("id").toString();
							// 相册名
							String name = obj.get("name").toString();
							// 贴图相册、新建相册、头像相册 过滤掉
							if (name.indexOf("贴图相册") > -1 || name.indexOf("新建相册") > -1 || name.indexOf("QQ秀") > -1 || name.indexOf("头像相册") > -1) {
								continue;
							}
							// System.out.println(allowAccess + "----" + name + "-----" + id);
							ablumStr.add(id + "***|***" + name);
						}
					}
					break;
				}
			} catch (Exception e) {
				// e.printStackTrace();
				continue;
			}
		}

		/******************************************************************************************
		 * 接下来依次访问每个相册 http://hzplist.photo.qq.com/fcgi-bin/fcg_list_photo_v2?inCharset=gbk&outCharset=gbk&hostUin=315597239&notice=0&callbackFun=&format=jsonp&plat=qzone&source=qzone&appid=4&uin=461504978&albumid=V10rZ6LV1hOlI9&t=0.0017526895933080988&g_tk=1446409337
		 ******************************************************************************************/

		if (ablumStr != null && ablumStr.size() > 0) {
			int count = 0;
			for (String str : ablumStr) {
				++count;
				String[] s = str.split("\\*\\*\\*\\|\\*\\*\\*");
				String albumid = s[0];
				String albumname = s[1];
				// System.out.println(albumid);

				for (int i = 1; i < 2; i++) {
					// 轮询前两个前缀直接跳出 默认服务器
					if (i == 2) {
						break;
					}
					get = new HttpGet("http://" + list.get(i) + ".photo.qq.com/fcgi-bin/fcg_list_photo_v2?inCharset=gbk&outCharset=gbk&hostUin=" + targetname + "&notice=0&callbackFun=&format=jsonp&plat=qzone&source=qzone&appid=4&uin=" + username + "&albumid=" + albumid + "&t=" + Math.random() + "&g_tk=1446409337");
					get.setHeader("Accept", "application/javascript, */*;q=0.8");
					get.setHeader("Accept-Encoding", "gzip, deflate");
					get.setHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");

					JSONObject json2 = null;
					try {
						response = client.execute(get);
						entity = EntityUtils.toString(new GzipDecompressingEntity(response.getEntity()));
						entity = entity.substring(10, entity.length() - 2);
						json2 = new JSONObject(entity);
						json2 = (JSONObject) json2.get("data");
						JSONArray array2 = json2.getJSONArray("pic");
						if (array2 != null && array2.length() > 0) {
							// System.out.println("****************************************");
							// System.out.println("相册"+count+"的图片在" + list.get(i) + "前缀服务器上,共有"+array2.length()+"张");
							for (int j = 0; j < array2.length(); j++) {
								JSONObject obj = (JSONObject) array2.get(j);
								// 图片真实地址
								String pUrl = obj.getString("url");
								// System.out.println(pUrl);
								pUrl = StrUtil.parse(pUrl);
								QQBean bean = new QQBean(pUrl, albumname, targetname, j, sex, array2.length());
								// 保存这张图片
								pList.add(bean);
							}
							break;
						}
					} catch (Exception e) {
						// e.printStackTrace();
						continue;
					}
				}
			}
			if (pList != null && pList.size() > 0) {
				upload(targetname);
			}
		}
		// }
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值