找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 3013|回复: 1

自己动手写爬虫下载notepad++官网代码

[复制链接]

311

主题

228

回帖

7373

积分

用户组: 真·技术宅

UID
2
精华
76
威望
291 点
宅币
5619 个
贡献
253 次
宅之契约
0 份
在线时间
952 小时
注册时间
2014-1-25
发表于 2014-2-21 14:17:43 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
  1. import java.io.File;import java.io.FileOutputStream;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.net.URL;
  5. import java.net.URLConnection;

  6. import org.jsoup.Jsoup;
  7. import org.jsoup.nodes.Document;
  8. import org.jsoup.nodes.Element;
  9. import org.jsoup.select.Elements;

  10. public class notepadplusplus
  11. {
  12.         public static String url="http://svn.tuxfamily.org";
  13.         public static String path="k:/notepad++";
  14.         public static int filethreadnum=0;
  15.        
  16.         public static boolean setinit=false;//是否强制初始化
  17.         public static String[] initstring={};//初始化目录位要开始更新的目录,按深度顺序
  18.         public static int curdepth=0;//当前初始化深度

  19.         public static class FileThread extends Thread
  20.         {
  21.                 String curnode;
  22.                 String filepath;
  23.                
  24.                 FileThread(String curnode,String filepath)
  25.                 {
  26.                         this.curnode=curnode;
  27.                         this.filepath=filepath;
  28.                 }
  29.                
  30.                 public void run()
  31.                 {
  32.                         try
  33.                         {
  34.                                 while(filethreadnum>30)
  35.                                 {
  36.                                         sleep(1000);
  37.                                 }
  38.                                 filethreadnum++;
  39.                                
  40.                                 int byteread=0;
  41.                                 int bytesum=0;
  42.                                 URL weburl=new URL(url+curnode.replaceAll("view=.*$","view=co"));
  43.                                 URLConnection con=weburl.openConnection();
  44.                                 InputStream instream=con.getInputStream();
  45.                                 FileOutputStream fs=new FileOutputStream((path+filepath).replace("%20"," "));
  46.                                 byte[] buffer=new byte[65536];
  47.                                 while((byteread=instream.read(buffer)) != -1)
  48.                                 {
  49.                                          bytesum+=byteread;
  50.                                          fs.write(buffer,0,byteread);
  51.                                          System.out.println("\t\t当前下载文件:"+filepath+curnode+"\t当前大小:"+bytesum);
  52.                                 }
  53.                                 fs.close();
  54.                                 instream.close();

  55.                                 filethreadnum--;
  56.                         }
  57.                         catch(Exception e)
  58.                         {
  59.                                 System.out.println("error");
  60. //                                new File(path+filepath).deleteOnExit();;
  61.                         }
  62.                 }
  63.         }
  64.        
  65.         public static String escape(String src)
  66.         {
  67.                 StringBuffer sbuf=new StringBuffer();
  68.                 int len=src.length();
  69.                 for(int i=0;i<len;i++)
  70.                 {
  71.                         int ch=src.charAt(i);
  72.                         if(ch == '\\' || ch == '*' || ch == '?' || ch == '"' || ch == '<' || ch == '>' || ch == '|')
  73.                                 sbuf.append('x');//忽略不能做文件名的字符
  74.                         else
  75.                                 sbuf.append(ch);
  76.                 }
  77.        
  78.                 return sbuf.toString();
  79.         }
  80.        
  81.         public static String createFolder(String folderPath)
  82.         {
  83.                 String txt = folderPath;
  84.                 txt.replace('\\','/');
  85.                 if(txt.charAt(txt.length()-1) != '/')
  86.                         txt+='/';
  87.                 try
  88.                 {
  89.                         File myFilePath = new File(txt);
  90.                         txt = folderPath;
  91.                         if (!myFilePath.exists())
  92.                         {
  93.                                 if(!myFilePath.mkdir())
  94.                                 {
  95.                                         String newpath=folderPath.substring(0,folderPath.length()-1);
  96.                                         newpath=newpath.substring(0,newpath.lastIndexOf('/'));
  97.                                         createFolder(newpath);
  98.                                         myFilePath.mkdir();
  99.                                 }
  100.                         }
  101.                 }
  102.                 catch (Exception e)
  103.                 {
  104.                         System.out.println("错误!");
  105.                 }
  106.                 return txt;
  107.         }
  108.        
  109.         public static void myresolve(Element e) throws IOException
  110.         {
  111.                 try
  112.                 {
  113.                         String curnode=e.attr("href");
  114.                         System.out.println(curnode);
  115.                         if(setinit)
  116.                         {
  117.                                 if(!curnode.equals(initstring[curdepth]))
  118.                                         return;
  119.                                 else
  120.                                         curdepth++;
  121.                                 if(curdepth >= initstring.length)
  122.                                         setinit=false;
  123.                         }
  124.           
  125.                         if(!curnode.contains(".."))
  126.                         {//非父目录
  127.                                 if(curnode.charAt(curnode.length()-1) == '/')
  128.                                 {//目录
  129.                                         createFolder((path+curnode).replace("%20"," "));
  130.                                         Document doc=Jsoup.connect(url+curnode).timeout(0).get();
  131.                                         System.out.println("当前目录:"+url+curnode);
  132.                                         Elements items = new Elements();
  133.                                         Elements items1=doc.select("tr.vc_row_even td[style] a");
  134.                                         items.addAll(items1);
  135.                                         items1=doc.select("tr.vc_row_odd td[style] a");
  136.                                         items.addAll(items1);
  137.                                         for(Element ele:items)
  138.                                         {
  139.                                                 myresolve(ele);
  140.                                         }
  141.                                 }
  142.                                 else
  143.                                 {//文件
  144.                                         String filepath=curnode.substring(0,curnode.lastIndexOf('/')+1)+e.attr("name");
  145.                                         File curfile=new File((path+filepath).replace("%20"," "));
  146.                                         if(curfile.exists())
  147.                                                 return;
  148.                                         while(filethreadnum>30)
  149.                                         {
  150.                                                 Thread.sleep(1000);
  151.                                         }
  152.                                         (new FileThread(curnode,filepath)).start();
  153.                                 }
  154.                         }
  155.                 }
  156.                 catch(Exception exc)
  157.                 {
  158.                         System.out.println("错误!");
  159.                 }
  160.         }
  161.        
  162.         public static void main(String[] args) throws IOException
  163.         {
  164.                 try
  165.                 {
  166.                         Document doc = Jsoup.connect(url+"/").timeout(0).get();
  167.                         Elements items=doc.select("tr.vc_row_even a");
  168.                         items.addAll(doc.select("tr.vc_row_odd a"));
  169.                         createFolder(path);
  170.                         for(Element e:items)
  171.                         {
  172.                                 myresolve(e);
  173.                         }
  174.                 }
  175.                 catch(Exception exc)
  176.                 {
  177.                         System.out.println("错误!");
  178.                 }
  179.         }
  180. }
复制代码


回复

使用道具 举报

1113

主题

1654

回帖

7万

积分

用户组: 管理员

一只技术宅

UID
1
精华
245
威望
744 点
宅币
24304 个
贡献
46222 次
宅之契约
0 份
在线时间
2302 小时
注册时间
2014-1-26
发表于 2014-2-21 16:47:22 | 显示全部楼层
为毛每次都要我帮你重新排版哟……我成擦屁股的了
回复 赞! 靠!

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-5-14 16:09 , Processed in 0.040974 second(s), 30 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表