如何抓取网站页面内容

      很多时候,我们想获取一些网页的内容,可以运用以下几种方法:

HTTPCLIENT

       get方法:

      
HttpClient httpClient = new HttpClient();  
       GetMethod getMethod = new GetMethod("http://www.baidu.com/");  
       try {  
           int statusCode = httpClient.executeMethod(getMethod);  
           if (statusCode != HttpStatus.SC_OK) {  
               System.err.println("Method failed: "  
                       + getMethod.getStatusLine());  
           }  
           // 读取内容  
           byte[] responseBody = getMethod.getResponseBody();  
           // 处理内容  
     String html = new String(responseBody);  
     System.out.println(html);   
       } catch (Exception e) {  
           System.err.println("页面无法访问");  
       }finally{  
        getMethod.releaseConnection();  
    }  

        post方法:


HttpClient httpClient = new HttpClient();  
       PostMethod postMethod = new PostMethod(UrlPath);  
       postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());  
       NameValuePair[] postData = new NameValuePair[2];  
       postData[0] = new NameValuePair("username", "xkey");  
       postData[1] = new NameValuePair("userpass", "********");  
       postMethod.setRequestBody(postData);  
       try {  
           int statusCode = httpClient.executeMethod(postMethod);  
           if (statusCode == HttpStatus.SC_OK) {  
               byte[] responseBody = postMethod.getResponseBody();  
               String html = new String(responseBody);  
               System.out.println(html);  
           }  
       } catch (Exception e) {  
           System.err.println("页面无法访问");  
       }finally{  
        postMethod.releaseConnection();  
    }  


wget命令

       使用命令: wget -c -r -np -k -p http://blog.csdn.net/lifen0908/article/details/45866853  
       
       具体使用命令:官网文档

Teleport Pro软件

        安装之后直接文件-新项目向导,下一步下一步,填上网址。然后点击标签run就可以了。具体软件下载地址:Teleport Pro

          

      
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值