网络爬虫之get方法
来源:互联网 发布:java serversocket 编辑:程序博客网 时间:2024/05/16 12:14
使用爬虫的工具的是利用myeclipse和Apache来实现,其中,Apache是有多个版本的,多个版本之间有很多函数和类是不同的,需要根据具体使用的版本号来选取对应的函数。这里使用的是Apache4.3。具体实现代码如下:
package test123;///////////////////////////////////////////Date:2016.2.19//Writer:P.C.//Description: 使用Get方法获取网页源码////////////////////////////////////////import java.io.IOError;import java.io.IOException;import org.apache.http.HttpEntity;import org.apache.http.HttpRequest;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;//import org.apache.http.impl.client.DefaultHttpClient;//本类包内的内容可以使用,所以画横线,但已不推荐使用import org.apache.http.util.EntityUtils;//public class initial {//public static void main(String[] argx){//String path="http://www.***.com";//System.out.println(path);//try {// URL pageURL=new URL("http://www.baidu.com");// } catch (Exception e) {// e.printStackTrace();// }//System.out.println(path);//}////}@SuppressWarnings("deprecation")public class initial{public static void main(String[] argx){String result = null;CloseableHttpClient httpClient=HttpClients.createDefault();//创建一个客户端,类似于浏览器HttpGet getmethod=new HttpGet("http://www.***.com");//创建一个get方法,类似于在浏览器中输入网址try{HttpResponse httpResponse=httpClient.execute(getmethod);//相当于在输入网址后敲回车if(httpResponse.getStatusLine().getStatusCode()==200)//状态码,200表示请求成功{HttpEntity httpEntity = httpResponse.getEntity(); //即可以是数据流也可以是字符串形式,根据需要设定 result = EntityUtils.toString(httpEntity);//EntityUtils对象是org.apache.http.util下的一个工具类,用官方的解释是为HttpEntity对象提供的静态帮助类 //result.replaceAll("\r", "");}else//当页面未正确打开时,如响应吗为201……,显示错误{result="ERROE";}System.out.println(result);//输出结果,可以与原网址的源码进行对比,如取其中的一句进行比对}catch(ClientProtocolException e){//客户端协议异常e.printStackTrace(); result = e.getMessage().toString(); System.out.println(result);}catch(IOException e){e.printStackTrace(); result = e.getMessage().toString(); System.out.println(result);}}}
0 0
- 网络爬虫之get方法
- 网络异步Get请求之block方法
- HttpURL网络获取资源之:GET方法
- python爬虫之GET请求
- Spider网络爬虫学习——通过Get、Post方法直接抓取网页内容
- 网络爬虫学习笔记——GET方法下几种常用情况
- 网络爬虫之初识网络爬虫
- 网络爬虫之Spider
- 搜索引擎之网络爬虫
- Python之网络爬虫
- 网络爬虫之Spider
- Python--之网络爬虫
- 搜索引擎之网络爬虫
- Python之网络爬虫
- 网络爬虫之beautifulsoup
- 网络爬虫之lxml
- 网络爬虫之开篇
- Jsoup之网络爬虫
- 理解inode
- Java代码中的特殊注释
- [神经网络]1.7-Using neural nets to recognize handwritten digits-Toward deep learning(翻译)
- Android ListView局部刷新
- 好用的产品设计工具之 Axure
- 网络爬虫之get方法
- 八大排序算法实现
- PHP 大小写转换函数 lcfirst ucfirst ucwords strtolower strtoupper
- Android DeskClock源码分析之旅---AndroidManifest.xml文件
- 缺页中断
- 接口回调
- Android提示版本更新
- iOS设计UI工具大全
- Docker基础操作