爬虫

 1 import java.io.*;
 2 import java.net.*;
 3 import java.util.regex.*;
 4 public class Main {
 5  static String SendGet(String url) {
 6   // 定义一个字符串用来存储网页内容
 7   String result = "";
 8   // 定义一个缓冲字符输入流
 9   BufferedReader in = null;
10   try {
11    // 将string转成url对象
12    URL realUrl = new URL(url);
13    // 初始化一个链接到那个url的连接
14    URLConnection connection = realUrl.openConnection();
15    // 开始实际的连接
16    connection.connect();
17    // 初始化 BufferedReader输入流来读取URL的响应
18    in = new BufferedReader(new InputStreamReader(
19      connection.getInputStream()));
20    // 用来临时存储抓取到的每一行的数据
21    String line;
22    while ((line = in.readLine()) != null) {
23     // 遍历抓取到的每一行并将其存储到result里面
24     result += line;
25    }
26   } catch (Exception e) {
27    System.out.println("发送GET请求出现异常!" + e);
28    e.printStackTrace();
29   }
30   // 使用finally来关闭输入流
31   finally {
32    try {
33     if (in != null) {
34      in.close();
35     }
36    } catch (Exception e2) {
37     e2.printStackTrace();
38    }
39   }
40   return result;
41  }
原文地址:https://www.cnblogs.com/fg-fd/p/7059004.html