import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TiebaID {
public static void main(String[] args) {
HttpURLConnection httpUrlConnection = null;
InputStream is = null;
BufferedReader br = null;
String url = null;
StringBuffer sb;
try {
url = "http://tieba.baidu.com/f?kw=java";
} catch (Exception e) {
e.printStackTrace();
}
try {
httpUrlConnection = (HttpURLConnection) new URL(url)
.openConnection(); // 创建连接
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
httpUrlConnection.setRequestMethod("GET");
} catch (ProtocolException e) {
e.printStackTrace();
}
try {
httpUrlConnection.connect(); // 建立连接
} catch (IOException e) {
e.printStackTrace();
}
try {
is = httpUrlConnection.getInputStream(); // 读取输入流
sb = new StringBuffer();
br = new BufferedReader(new InputStreamReader(is));
String string;
while ((string = br.readLine()) != null) {
sb.append(string);
}
string = "<a href=\"/i/sys/jump.*?</a>";
Pattern pattern = Pattern.compile(string);
Pattern pattern1 = Pattern.compile(">.*<");
Matcher matcher = pattern.matcher(sb.toString());
HashSet<String> hs = new HashSet<String>();
while (matcher.find()) {
String s = matcher.group();
Matcher m = pattern1.matcher(s);
if (m.find()) {
String id = m.group();
hs.add(id.substring(1, id.length() - 1));
}
}
for (String s : hs) {
System.out.println("@" + s);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
is.close();
httpUrlConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
一个简单的获取百度贴吧上ID的java程序
最新推荐文章于 2021-08-10 08:37:12 发布