zoukankan      html  css  js  c++  java
  • 网络爬虫简单实现

    有访问权限

    public class SpiderTest {
      public static void main(String[] args) throws IOException {
        URL url = new URL("https://www.baidu.com");
        InputStream is = url.openStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
        String msg = null;
        while(null!=(msg=br.readLine())){
          System.out.println(msg);
        }
        br.close();
      }
    }

    无访问权限

    public class SpiderTest2 {
      public static void main(String[] args) throws IOException {
        URL url = new URL("https://www.jd.com");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/67.0");
        InputStream is = url.openStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
        String msg = null;
        while(null!=(msg=br.readLine())){
          System.out.println(msg);
        }
        br.close();
      }
    }

  • 相关阅读:
    认识计算机
    Sum 类型题目总结
    3Sum Smaller 解答
    3Sum Closest 解答
    Roman to Integer && Integer to Roman 解答
    Longest Common Prefix 解答
    Shortest Word Distance 解答
    Longest Valid Parentheses 解答
    Lowest Common Ancestor of a Binary Search Tree 解答
    Longest Palindromic Substring 解答
  • 原文地址:https://www.cnblogs.com/5aixin/p/11094702.html
Copyright © 2011-2022 走看看