老的网易相册爬虫

 最近网易相册改革了,有很多的相册都改成了新的版本,这就苦了一些以前的网易相册获取工具(包括我用ajax写的一个);虽然自己写的这个程序是有点过时了,但是这是我用java写的第一个类似爬虫的东西(其实并不是这样的),以后在有时间和有兴趣时,我会研究新的网易相册,写出一个真正的爬虫出来;

相关说明,我把获得的相册列表文件放到了C盘根目录(可以修改代码37行),在等10行中为相册名赋值;现在这个工具对某些没有升级的相册还是有用的;本工具就是分析了"http://photo.163.com/js/albumsinfo.php?user=username&from=guest";这个文件

其中汉字处理还有问题

 

好的现在给出源代码吧:

  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4. import java.util.regex.*;
  5. public class socket {
  6.     public static void main(String[] args) {
  7.         String username = "";  //这里写上你想要获取相册的用户名
  8.         String urlName = "http://photo.163.com/js/albumsinfo.php?user="
  9.                 + username + "&from=guest";
  10.         String content = getContent(urlName);
  11.         // 正则表达式处理
  12.         String re = "gAlbumsIds//[([0-9]*)[^//d]*(//d+)[^,]*,[^,]*,(//d*),/"(.*?)/",/"(.*?)/"//]";
  13.         List<AlbumsInfo> list = getContentListA(re, content);
  14.         System.out.println("begin aaa!/r/n content's length is:"
  15.                 + content.length());
  16.         for (AlbumsInfo album : list) {
  17.             urlName = "http://photo.163.com/js/photosinfo.php?user=" + username
  18.                     + "&aid=" + album.getListId() + "&from=guest";
  19.             content = getContent(urlName);
  20.             printAlbum(album);
  21.             re = "([^/"]+?)/"//];";
  22.             List<String> l = getContentList(re, content);
  23.             String fileContent = l.toString().replaceAll("[//[//]]""")
  24.                     .replaceAll("//s*,//s*""/r/n");
  25.             fileContent += "/r/n/r/n===============相关信息=========================/r/n"
  26.                     + album.getNote();
  27.             writeToFile("c://" + album.getTitle() + ".txt", fileContent);  //文件放在C盘根目录
  28.         }
  29.         System.out.print("end aaa!/r/n");
  30.     }
  31.     public static void printAlbum(AlbumsInfo album) {
  32.         System.out.print("/r/n========/r/nId: " + album.getId() + "/r/nListId: "
  33.                 + album.getListId() + "/r/nnum : " + album.getTitle()
  34.                 + "/r/ntitle : " + album.getTitle() + "/r/nNote : "
  35.                 + album.getNote());
  36.     }
  37.     public static void writeToFile(String fileName, String content) {
  38.         try {
  39.             FileWriter out = new FileWriter(fileName);
  40.             out.write(content);
  41.             out.close();
  42.         } catch (IOException e) {
  43.             e.printStackTrace();
  44.         }
  45.     }
  46.     public static List<String> getContentList(String re, String content) {
  47.         Pattern p = Pattern.compile(re);
  48.         Matcher m = p.matcher(content);
  49.         List<String> list = new LinkedList<String>();
  50.         while (m.find())
  51.             list.add(m.group(1));
  52.         return list;
  53.     }
  54.     public static List<AlbumsInfo> getContentListA(String re, String content) {
  55.         List<AlbumsInfo> list = new LinkedList<AlbumsInfo>();
  56.         Pattern p = Pattern.compile(re);
  57.         Matcher m = p.matcher(content);
  58.         while (m.find()) {
  59.             AlbumsInfo temp = new AlbumsInfo();
  60.             temp.setId(Integer.parseInt(m.group(1)));
  61.             temp.setListId(m.group(2));
  62.             temp.setNum(m.group(3));
  63.             temp.setTitle(changeStr(m.group(4)));
  64.             temp.setNote(m.group(5));
  65.             list.add(temp);
  66.         }
  67.         return list;
  68.     }
  69.     public static String changeStr(String str) {
  70.         char[] temp = str.toCharArray();
  71.         return temp.toString();
  72.     }
  73.     public static String getContent(String urlName) {
  74.         StringBuilder content = new StringBuilder();
  75.         try {
  76.             URL url = new URL(urlName);
  77.             InputStream inStream = url.openStream();
  78.             Scanner in = new Scanner(inStream);
  79.             while (in.hasNext()) {
  80.                 content.append(in.next() + "/r/n");
  81.             }
  82.         } catch (IOException e) {
  83.             e.printStackTrace();
  84.         }
  85.         return content.toString();
  86.     }
  87. }

 

  1. public class AlbumsInfo
  2.     {
  3.         private int id=0;
  4.         private String listId="";
  5.         private String num="";
  6.         private String title="";
  7.         private String note="";
  8.         
  9.         public AlbumsInfo()
  10.         {
  11.         }
  12.         public int getId() {
  13.             return id;
  14.         }
  15.         public void setId(int id) {
  16.             this.id = id;
  17.         }
  18.         public String getNum() {
  19.             return num;
  20.         }
  21.         public void setNum(String num) {
  22.             this.num = num;
  23.         }
  24.         public String getTitle() {
  25.             return title;
  26.         }
  27.         public void setTitle(String title) {
  28.             this.title = title;
  29.         }
  30.         public String getNote() {
  31.             return note;
  32.         }
  33.         public void setNote(String note) {
  34.             this.note = note;
  35.         }
  36.         public String getListId() {
  37.             return listId;
  38.         }
  39.         public void setListId(String listId) {
  40.             this.listId = listId;
  41.         }
  42.     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值