[Java] java采集网页 抓取网页 →→→→→进入此内容的聊天室

来自 , 2019-07-20, 写在 Java, 查看 97 次.
URL http://www.code666.cn/view/5ea1649a
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7.  
  8. /**
  9.  * java采集网页
  10.  *
  11.  */
  12. public class HttpWebCollecter {
  13.  
  14.         /**
  15.          * 网页抓取方法
  16.          *
  17.          * @param urlString
  18.          *            要抓取的url地址
  19.          * @param charset
  20.          *            网页编码方式
  21.          * @param timeout
  22.          *            超时时间
  23.          * @return 抓取的网页内容
  24.          * @throws IOException
  25.          *             抓取异常
  26.          */
  27.         public static String GetWebContent(String urlString, final String charset,
  28.                         int timeout) throws IOException {
  29.                 if (urlString == null || urlString.length() == 0) {
  30.                         return "";
  31.                 }
  32.                 urlString = (urlString.startsWith("http://") || urlString
  33.                                 .startsWith("https://")) ? urlString : ("http://" + urlString)
  34.                                 .intern();
  35.                 URL url = new URL(urlString);
  36.                 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  37.                 conn.setDoOutput(true);
  38.                 conn.setRequestProperty("Pragma", "no-cache");
  39.                 conn.setRequestProperty("Cache-Control", "no-cache");
  40.  
  41.                 int temp = Integer.parseInt(Math.round(Math.random()
  42.                                 * (UserAgent.length - 1))
  43.                                 + "");
  44.                 conn.setRequestProperty("User-Agent", UserAgent[temp]); // 模拟手机系统
  45.                 conn.setRequestProperty("Accept",
  46.                                 "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");// 只接受text/html类型,当然也可以接受图片,pdf,*/*任意,就是tomcat/conf/web里面定义那些
  47.                 conn.setConnectTimeout(timeout);
  48.                 try {
  49.                         if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
  50.                                 return "";
  51.                         }
  52.                 } catch (Exception e) {
  53.                         try {
  54.                                 System.out.println(e.getMessage());
  55.                         } catch (Exception e2) {
  56.                                 e2.printStackTrace();
  57.                         }
  58.                         return "";
  59.                 }
  60.                 InputStream input = conn.getInputStream();
  61.                 BufferedReader reader = new BufferedReader(new InputStreamReader(input,
  62.                                 charset));
  63.                 String line = null;
  64.                 StringBuffer sb = new StringBuffer("");
  65.                 while ((line = reader.readLine()) != null) {
  66.                         sb.append(line).append("\r\n");
  67.                 }
  68.                 if (reader != null) {
  69.                         reader.close();
  70.                 }
  71.                 if (conn != null) {
  72.                         conn.disconnect();
  73.                 }
  74.                 return sb.toString();
  75.         }
  76.  
  77.         public static String[] UserAgent = {
  78.                         "Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.2",
  79.                         "Mozilla/5.0 (iPad; U; CPU OS 3_2_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B500 Safari/531.21.11",
  80.                         "Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18121",
  81.                         "Nokia5700AP23.01/SymbianOS/9.1 Series60/3.0",
  82.                         "UCWEB7.0.2.37/28/998", "NOKIA5700/UCWEB7.0.2.37/28/977",
  83.                         "Openwave/UCWEB7.0.2.37/28/978",
  84.                         "Mozilla/4.0 (compatible; MSIE 6.0; ) Opera/UCWEB7.0.2.37/28/989" };
  85.  
  86. }

回复 "java采集网页 抓取网页"

这儿你可以回复上面这条便签

captcha