上班看糗百【JAVA修改版】

原创
2013/10/09 15:07
阅读数 89
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Scanner;
public class QiuShiBaiKe {
    /**
     * @param args
     */
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("^\\d{1,}$");
        StringBuffer sb = null;
        System.out.println("Please input the page( input 0 to exit):");
        Scanner sc = new Scanner(System.in);
        int page = -1;
        String next;
        while(page != 0) {
            next = sc.next();
            if(pattern.matcher(next).matches()) {
                page = Integer.parseInt(next);
                if ( page <= 0) {
                    continue;
                }
                sb = getUrl(page);
                System.out.println(sb);
            }
            System.out.println("Please input the page( input 0 to exit):");
        }
        sc.close();
    }
     
    private static StringBuffer getUrl(int page) {
        StringBuffer sb = new StringBuffer();
        try {
            URL url = new URL("http://www.qiushibaike.com/month/page/"+page+"?s=4595690&slow");
            InputStreamReader in = new InputStreamReader(url.openStream(),"UTF-8");
            BufferedReader br = new BufferedReader(in);
            String line;
            while((line = br.readLine())!=null) {
                if(line!=null && !line.isEmpty() &&line.charAt(0) == '<' ){
                    continue;
                }
                if(line.startsWith("糗友") || line.startsWith("棉柔版")) {
                    sb.append("\n");
                }
                sb.append(line.replaceAll("<br/>", "\n"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb;
    } 
}
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部