JAVA抓取网页的简单实现
2013-12-06 23:01
375 查看
最近在做java的爬虫,由于刚开始的时候使用的是httpclient,但是逐渐发现,有的功能不能实现,因此,自己利用java的net包做了一个爬虫,实现网页的基本抓取,其中考虑了浏览器的伪装,gzip格式的解码等困扰比较久的问题。代码如下
/** * @author houlaizhexq * @function:依靠java自己的net包实现的爬虫,解决浏览器伪装,gzip解码等问题 * @time:2013年12月6日 星期五 */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.zip.GZIPInputStream; public class Test5 { public static void main(String args[]){ URL url; try { url = new URL("http://www.baidu.com"); HttpURLConnection httpClientConnection=(HttpURLConnection) url.openConnection(); // 模仿IE的浏览器信息,防止网站禁止抓取网页 // Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */* // Accept-Language: zh-cn // Accept-Encoding: gzip, deflate // User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) // Host: 192.168.109.130 // Connection: close httpClientConnection.addRequestProperty("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*"); httpClientConnection.addRequestProperty("Accept-Language", "zh-cn"); httpClientConnection.addRequestProperty("Accept-Encoding", "gzip, deflate"); httpClientConnection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); httpClientConnection.addRequestProperty("Connection", "close"); //设置为get方法,默认即为get方法 httpClientConnection.setRequestMethod("GET"); // //如果是表单的提交之类的需要设置为POST方法,如下 // //因为需要写数据,所以设置为true // httpClientConnection.setDoOutput(true); // httpClientConnection.setRequestMethod("POST"); // //设置表单内的数据 // String username="user=admin"; // httpClientConnection.getOutputStream().write(username.getBytes()); // //写入服务器 // httpClientConnection.getOutputStream().flush(); // //关闭写出流 // httpClientConnection.getOutputStream().close(); httpClientConnection.connect(); //提取返回头信息 System.out.println(httpClientConnection.getResponseCode()); for(int j=1;;j++){ String header=httpClientConnection.getHeaderField(j); if(header==null){ break; } System.out.println(httpClientConnection.getHeaderFieldKey(j)+":"+header); } //获取返回体正文信息 BufferedReader bufferedReader=null; InputStream inputStream=null; //查看encoding是不是gzip,是的话,先解压成inputstream正常文件流,否则,不需要转换 if(httpClientConnection.getContentEncoding()!=null){ String encode=httpClientConnection.getContentEncoding().toLowerCase(); if(encode.indexOf("gzip") >= 0){ //转化gzip inputStream = new GZIPInputStream(httpClientConnection.getInputStream()); }else{ inputStream = httpClientConnection.getInputStream(); } } if(inputStream!=null){ bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8")); String line = null; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } } //关闭流 inputStream.close(); bufferedReader.close(); //断开连接 httpClientConnection.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }注意:其中的gzip主要是用来解决压缩的问题,以减少流量,其中返回的是否gzip主要根据你发送的请求Accept-Encoding字段判断,由于浏览器都会自动解码,所以一般返回gzip格式,但是自己的爬虫,如果不声明能接受gzip编码,则会返回正常的编码,这样在抓取的返回头文件不会出现content-encoding:gzip字段
相关文章推荐
- Java实现简单网页抓取
- java简单实现爬虫、jsoup实现网页抓取、POI实现数据导出Excel
- 简单的java爬虫抓取网页实现代码(未测试)
- java简单网页抓取的实现方法
- 利用Java实现简单的抓取网页数据并存放于plist中
- java利用url实现网页内容的抓取
- PHP实现简单爬虫-抓取网页url
- Java+Tomcat+MySQL实现简单的网页注册和登录
- Java两种方式简单实现:爬取网页并且保存
- 用JAVA实现简单爬虫循环抓取
- Python简单实现网页内容抓取功能示例
- java简单的抓取网页数据
- java爬虫实战简单用Jsoup框架进行网页爬虫(如抓取网页图片)
- 学习数据结构搜索的的意外收获哈。一个简单的基于java的网页抓取程序。
- 用JAVA实现简单爬虫多线程抓取
- Java实现的简单网页截屏功能示例
- Python 中利用urllib2简单实现网页抓取
- java利用url实现网页内容的抓取
- JAVA Web开发实现网页的简单打印
- java实现一个简单的HTTP服务器,带打开网页和计算功能