zoukankan      html  css  js  c++  java
  • 扒网页第一弹

      昨天下午抄的代码,用来完成扒网页这个步骤。

      仅能实现获取源代码的内容,还没有进行处理,今天将会完成处理的步骤。

      本次主要利用URLConnection 在Eclipse下完成 。

      有一个错误阻碍了我将近两个小时,即 URLConnection url_con =  url.openConnection(); 总是在报类型不匹配的错误。多次百度之后也没找到答案。最后是在与原博主的源代码的比对中发现了错误,是有一个包导错了。。。。。。。这个包是java.net.URLConnection;

      以下是我的代码:

      

      1 package tixiJG;
      2 
      3 import java.io.BufferedReader;
      4 import java.io.FileOutputStream;
      5 //import java.io.FileWriter;
      6 import java.io.IOException;
      7 import java.io.InputStream;
      8 import java.io.InputStreamReader;
      9 import java.io.OutputStreamWriter;
     10 
     11 import java.net.MalformedURLException;
     12 import java.net.URL;
     13 import java.net.URLConnection;
     14 
     15 /***
     16  * 2017-10-13
     17 - * @author Administrator-
     18  *
     19  */
     20 
     21 public class test1 {
     22 
     23     public static void main(String[] args) {
     24         
     25         
     26         String filePath = "E:/eclipseWorkspace/tixiJG/TTest/001.txt";
     27         
     28         String url_str = "http://www.imau.edu.cn";
     29         
     30         URL url = null;
     31         try{
     32             //将String转化为url
     33             url = new URL(url_str);
     34             
     35         }catch(MalformedURLException e){
     36             e.printStackTrace();
     37         }
     38         
     39     /*    URL url = new URL("https://www.hao123.com");*/
     40         
     41         String charset = "utf-8";
     42         
     43         int sec_cont = 1000;
     44         try {
     45             //初始化一个链接到URL的链接
     46             URLConnection url_con =  url.openConnection();
     47             
     48             //url_con.connect();
     49             
     50             url_con.setDoOutput(true);
     51             
     52             url_con.setReadTimeout(10 * sec_cont);
     53             
     54             url_con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
     55             
     56             InputStream htm_in = url_con.getInputStream();
     57             
     58             String htm_str = InputStream2String(htm_in,charset);
     59             
     60             saveHtml(filePath,htm_str);
     61             
     62         }catch(IOException e) {
     63             e.printStackTrace();
     64         }
     65 
     66     }
     67     
     68     
     69     public static void saveHtml( String filepath, String str ) {
     70         
     71         try{
     72             
     73             OutputStreamWriter outs = new OutputStreamWriter(new FileOutputStream(filepath,true), "utf-8");
     74             
     75             outs.write(str);
     76             
     77             System.out.print(str);
     78             
     79             outs.close();
     80         }catch(IOException e) {
     81             System.out.println("Error at save html...");
     82             e.printStackTrace();
     83         }
     84     }
     85     
     86     
     87     public static String InputStream2String(InputStream in_st, String charset) throws IOException{
     88         
     89         BufferedReader buff = new BufferedReader(new InputStreamReader(in_st, charset));
     90         StringBuffer res = new StringBuffer();
     91         
     92         String line = "";
     93         
     94         while((line = buff.readLine()) != null) {
     95             
     96             res.append(line);
     97             
     98         }
     99         
    100         return res.toString();
    101     }
    102     
    103     
    104 
    105 }
    ————————————来自 大中国的智慧结晶
  • 相关阅读:
    flume 安装过程记录
    hive 动态分区插入
    hive 数据导入
    scala 学习笔记
    hive 日志
    Linux下设置和查看环境变量
    数据结构(4):图解递归调用机制
    使用数据结构栈实现表达式计算器
    数据结构(3):java使用数组模拟堆栈
    使用Vue和Element-ui的cdn模式临摹一个后台框架
  • 原文地址:https://www.cnblogs.com/guoqiaoqiao/p/7665705.html
Copyright © 2011-2022 走看看