30行代码写出JavaSe爬虫,适合刚学Java的人参考学习

package Test;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
      public static void main(String[] args) {
          URL url = null;
          try {
                  for (int i = 90; i < 122; i++) {
                          url = new URL("http://www.jinghangzz.com/content.php?id=" + i);

                          URLConnection connection = url.openConnection();

                          connection.connect();
                          connection.setConnectTimeout(1);
                          InputStream input = connection.getInputStream();

                          BufferedReader br = new BufferedReader(new InputStreamReader(input, "utf-8"));

                          String len = null;
                          Pattern nameregx = Pattern.compile("[\\u4e00-\\u9fa5-\\s]{2,}[:][\\u4e00-\\u9fa5-[+||*||\\s||:]-[a-zA-Z0-9]]{1,}");
                          while ((len = br.readLine()) != null) {

                                  Matcher m = nameregx.matcher(len);
                                  while (m.find()) {
                                          String name = m.group();
                                          if(name.equals("地址:郑州市郑东新区博学路平安大道正商学府广场B座2305")){
                                                  continue;
                                          }
                                          System.out.println(i+"--"+name);
                                  }
                          }
                          System.out.println("---------------------------------");
                  }
          } catch (MalformedURLException e) {
                  e.printStackTrace();
          } catch (IOException e) {
                  e.printStackTrace();
          }
  }
}
 

猜你喜欢

转载自blog.csdn.net/weixin_41722928/article/details/86496409