分享一段下載QQ用戶日志到本地的代碼 [轉日志不用進空間啦,輸入QQ號就可以下載對方任意一篇日志了]


很悲劇的說,又是被凍醒的,苦逼的程序員生活.凍手凍腳的敲代碼,真心傷不起.

繼上次圖解分析的騰訊空間日志真實路徑后,閑着沒事就寫了段下載騰訊空間日志的代碼.這年頭轉日志不用進空間啦,輸入QQ號就可以下載對方任意一篇日志了.

當然你開心就全部下載嘍.

實習方式很簡單,簡單的有些搞笑,大俠們勿噴啊, 但是功能還是搞定了.至於優化或者還有更好的方法后面再想想.貼出來與大家分享一下.

package org.crawler.picture.dennisit.action; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; /** * * @version : 1.1 * * @author : 蘇若年 <a href="mailto:DennisIT@163.com">發送郵件</a> * * @since : 1.0 創建時間: 2013-1-2 下午11:56:55 * * @function: 日志下載備份類 * */ public class BlogDownloadAction extends DownloadAction{ /** * 創建每一頁的用戶訪問數量 * @param qq 用戶QQ號碼 * @param pos 其實頁碼 * @param num 每頁顯示數量 默認為15,根據實際情況定 * @return */ public String createURLForPage(String qq,int pos,int num){ String baseStrBegin = "http://b11.qzone.qq.com/cgi-bin/blognew/get_abs?hostUin="+qq; String baseStrcont1 = "&blogType=0&cateName=&cateHex=&statYear=2013&reqInfo=7&pos=" + pos; String baseStrcont2 = "&num=" + num +"&sortType=0&absType=0&source=0&rand=0.8141584321856499&g_tk=5381&verbose=1&ref=qzone"; return baseStrBegin + baseStrcont1 + baseStrcont2; } /** * 獲取每一頁的日志ID集合 * @param qq * @param page * @param num * @return */ public List<String> getBlogIDListForEachPage(String qq,int page,int num){ List<String> lst = new ArrayList<String>(); String diaryURL = createURLForPage(qq,page,num); //System.out.println(diaryURL);
 URL url; try { url = new URL(diaryURL); URLConnection urlConnection = url.openConnection(); urlConnection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); InputStream is = url.openStream(); BufferedReader bufferReader = new BufferedReader(new InputStreamReader(is)); String rLine = null; int countNum = 9; while((rLine=new String(bufferReader.readLine()))!=null){ countNum++; if(rLine.contains("cateInfo")){ break; } if((countNum-18)%13==0){ //逐行讀取,獲取每一個日志對象
                    String blogId = ""; if(rLine.contains("blogId")){ String temp = rLine ; if(temp.contains("{")){ temp = rLine.substring(8); } blogId = getBlogINFO(temp); lst.add(blogId); } } } }catch (Exception e) { // TODO: handle exception
 } return lst; } public String getBlogINFO(String str){ String[] strArray = str.split(":|,"); /*for(int i=0; i<strArray.length; i++){ System.out.println("strArray[" +i+"]=" + strArray[i]); }*/
        if(strArray.length!=0){ return strArray[1]; } return null; } /** * 獲取每一頁用戶的日志URL集合 * @param qq * @param page * @return */ public List<String> getBlogURLListForEachPage(String qq, int page,int num){ List<String> idlst = getBlogIDListForEachPage(qq,page,num); List<String> urlSet = new ArrayList<String>(); String blogUrl = "http://user.qzone.qq.com/"+qq+"/blog/"; for(String id:idlst){ urlSet.add(blogUrl+id); } return urlSet; } /** * 第一頁用於發送數據包,獲取回應的數據包信息,根據回應包信息檢測日志總數 * @param qq * @return */ public String createFirstPageURL(String qq){ return createURLForPage(qq,0,15); } /** * 獲取日志總數核心方法 * @return */ public int getBlogCount(String qq){ int blogCount = 0; String diaryURL = createFirstPageURL(qq); //第一頁用於發送數據包,獲取回應的數據包信息,根據回應包信息檢測日志總數
 URL url; try { url = new URL(diaryURL); URLConnection urlConnection = url.openConnection(); urlConnection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); InputStream is = url.openStream(); BufferedReader bufferReader = new BufferedReader(new InputStreamReader(is)); String rLine = null; while((rLine=bufferReader.readLine())!=null){ if(rLine.contains("totalNum")){ blogCount = Integer.parseInt(BlogINFOUtil.getBlogINFO(rLine));        //獲取日志總數
 break; } } }catch (Exception e) { // TODO: handle exception
 } return blogCount; } /** * 獲取用戶的所有日志的日志ID * @param qq */ public List<String> allQQBlogID(String qq){ List<String> allBlogID = new ArrayList<String>(); int count = getBlogCount(qq); int pageCount = (count%15==0)?count/15:(count/15+1) ; for(int i=0; i<pageCount;i++){ List<String> lsts ; if(i==pageCount-1){ lsts = getBlogIDListForEachPage(qq, i*15,count-(i*15)); }else{ lsts = getBlogIDListForEachPage(qq, i*15,15); } allBlogID.addAll(lsts); } return allBlogID; } /** * 獲取用戶的所有日志訪問URL * @param qq */ public List<String> allQQBlogURL(String qq){ List<String> allURL = new ArrayList<String>(); //DiaryDownload dyd = new DiaryDownload();
        int count = getBlogCount(qq); System.out.println("日志總數為:" + count); int pageCount = (count%15==0)?count/15:(count/15+1) ; System.out.println("用戶日志頁數:" + pageCount); int show = 0; for(int i=0; i<pageCount;i++){ System.out.println(qq+"用戶的第"+(i+1)+"頁的日志信息"); System.out.println("----------------------------------------"); List<String> lsts ; if(i==pageCount-1){ lsts = getBlogURLListForEachPage(qq, i*15,count-(i*15)); }else{ lsts = getBlogURLListForEachPage(qq, i*15,15); } for(String str : lsts){ System.out.println(qq +"用戶的第"+(++show)+"篇日志訪問URL為:\t"+str); } allURL.addAll(lsts); System.out.println("----------------------------------------"); } return allURL; } /** * 創建包含日志內容的真正URL * @param qq * @param logId * @return */ public String createHaveContentBlogURL(String qq,String logId){ /*
         http://b11.qzone.qq.com/cgi-bin/blognew/blog_output_data?uin= 1325103287 &blogid= 1305125403 &styledm=ctc.qzonestyle.gtimg.cn&imgdm=ctc.qzs.qq.com&bdm=b.qzone.qq.com&mode=2 &numperpage=15 &blogseed=0.491407030262053&property=GoRE&timestamp=1357192365&dprefix=&g_tk=5381 &ref=qzone&v6=1&entertime=1357192364386&via=QZ.HashRefresh &pos=1305125403 */ String baseContURL = "http://b11.qzone.qq.com/cgi-bin/blognew/blog_output_data?uin=" + qq +"&blogid="+logId ; String baseCont1 = "&styledm=ctc.qzonestyle.gtimg.cn&imgdm=ctc.qzs.qq.com&bdm=b.qzone.qq.com&mode=2&numperpage=15"; String baseCont2 = "&blogseed=0.491407030262053&property=GoRE&timestamp=1357192365&dprefix=&g_tk=5381"; String baseCont3 = "&ref=qzone&v6=1&entertime=1357192364386&via=QZ.HashRefresh"; String baseCont4 = "&pos=" + logId; return baseContURL + baseCont1 + baseCont2 + baseCont3 + baseCont4; } /** * 備份日志 * @param backPath 日志存放路徑 * @param fileName 日志名稱 * @param urlStr 日志URL */ public static void backQQBlog(String backPath, String fileName, String urlStr){ URL url; try { url = new URL(urlStr); URLConnection urlConnection = url.openConnection(); urlConnection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); InputStream is = url.openStream(); BufferedReader bufferReader = new BufferedReader(new InputStreamReader(is,"gb2312")); String rLine = ""; File file = new File(backPath+fileName); FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); while((rLine=bufferReader.readLine())!=null){ System.out.println(rLine); bw.write("" +rLine.toString()+"\r\n"); //bw.write(new String(rLine.getBytes("GBK"),"gbk")+"\r\n");
 } is.close(); bufferReader.close(); bw.close(); fw.close(); }catch (Exception e) { e.printStackTrace(); }finally{ } } public static void main(String[] args) { BlogDownloadAction down = new BlogDownloadAction(); List<String> qqIdList = down.allQQBlogID("799089378"); List<String> qqBlogURLList = down.allQQBlogURL("799089378"); System.out.println("所有日志總數:" + qqBlogURLList.size()); //備份第2篇日志
        String filePath = "F:/"; String filename = "799089378_"+qqIdList.get(9)+".html"; System.out.println("第二篇日志的Id為:"+ qqIdList.get(9)+ ",\t日志訪問URL為:" +qqBlogURLList.get(6)); String url = down.createHaveContentBlogURL("799089378",qqIdList.get(9)); System.out.println(url); down.backQQBlog(filePath, filename, url); } }


轉載請注明出處[http://www.cnblogs.com/dennisit/archive/2013/01/05/2845095.html]

  在線交談


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM