JAVA获取贴吧帖号

package tieba;

/**
 * @author Cytosine
 * @thanks Wizard
 */

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PostIdentifier {
	public static void main(String[] args) {
		System.out.println("请输入贴吧名(不含“吧”字哦>,<)");
		Scanner InputName=new Scanner(System.in);
		String TiebaName=InputName.nextLine();
		System.out.println("请输入页数(不验证最大页数,请自行验证哦>,<)");
		Scanner InputListPage=new Scanner(System.in);
		String ListPage=InputListPage.nextLine();
		InputName.close();
		InputListPage.close();
		int PageNumber=Integer.parseInt(ListPage);
		int pnNumber=(PageNumber-1)*50;
		String pn=Integer.toString(pnNumber);
		//生成URL String
		StringBuilder url=new StringBuilder("http://tieba.baidu.com/f?kw=");
		url.append(TiebaName+"&pn="+pn);
		StringBuffer html=httpRequest(url.toString());
		//处理HTML 正则提取帖号
		String reg="/p/\\d+";
		Pattern p=Pattern.compile(reg);
		Matcher m=p.matcher(html);
		while(m.find()){
			String text=m.group(0).trim();
			if(!"".equals(text)){
				String[] PostIdentifier=text.split("p/");
				System.out.println(PostIdentifier[1]);
			}
		}
	}
	public static StringBuffer httpRequest(String url){
		StringBuffer result=new StringBuffer();
		BufferedReader bufferedReader=null;
		try{
			URL TiebaURL=new URL(url);
			URLConnection connection=TiebaURL.openConnection();
			connection.connect();
			bufferedReader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
		}catch(Exception e){
			e.printStackTrace();
		}
		try{
			String line;
			while((line=bufferedReader.readLine())!=null){
				result.append(line);
			}
			bufferedReader.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
}

你可能感兴趣的:(Java,Tieba,java,Tieba)