Java网页抓取例子
2011-01-23 00:23
281 查看
import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.PostMethod; public class RetrivePage { private static HttpClient httpClient = new HttpClient(); // 设置代理服务器 static { // 设置代理服务器的IP地址和端口 // httpClient.getHostConfiguration().setProxy("172.17.18.84", 8080); } public static boolean downloadPage(String path) throws HttpException, IOException { InputStream input = null; OutputStream output = null; // 得到post方法 PostMethod postMethod = new PostMethod(path); // 设置post方法的参数 /* * NameValuePair[] postData = new NameValuePair[2]; postData[0] = new * NameValuePair("name","lietu"); postData[1] = new * NameValuePair("password","*****"); * postMethod.addParameters(postData); */ // 执行,返回状态码 int statusCode = RetrivePage.httpClient.executeMethod(postMethod); // 针对状态码进行处理 (简单起见,只处理返回值为200的状态码) if (statusCode == HttpStatus.SC_OK) { input = postMethod.getResponseBodyAsStream(); //得到文件名 String filename = path.substring(path.lastIndexOf('/')+1); //获得文件输出流 output = new FileOutputStream(filename); //输出到文件 int tempByte = -1; while((tempByte=input.read())>0){ output.write(tempByte); } //关闭输入输出流 if(input!=null){ input.close(); } if(output!=null){ output.close(); } return true; } //若需要转向,则进行转向操作 if ((statusCode == HttpStatus.SC_MOVED_TEMPORARILY) || (statusCode == HttpStatus.SC_MOVED_PERMANENTLY) || (statusCode == HttpStatus.SC_SEE_OTHER) || (statusCode == HttpStatus.SC_TEMPORARY_REDIRECT)) { //读取新的URL地址 Header header = postMethod.getResponseHeader("location"); if(header!=null){ String newUrl = header.getValue(); if(newUrl==null||newUrl.equals("")){ newUrl="/"; //使用post转向 PostMethod redirect = new PostMethod(newUrl); //发送请求,做进一步处理。。。。。 } } } return false; } /** * 测试代码 */ public static void main(String[] args) { // 抓取lietu首页,输出 try { RetrivePage.downloadPage("http://www.baidu.com"); RetrivePage.downloadPage("http://www.renren.com"); RetrivePage.downloadPage("http://www.lietu.com"); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
必须导入的jar包:
相关文章推荐
- Java网页资源抓取例子(使用第三方包Jsoup解析Html)
- Java抓取网页信息[例子是抓取双色球某一天的信息]
- JAVA在线抓取网页中的元素的例子(使用JSoup包进行解析)
- JAVA在线抓取网页中的元素的例子(使用JSoup包进行解析)
- java 网页抓取数据
- java简单实现爬虫、jsoup实现网页抓取、POI实现数据导出Excel
- java使用正则抓取网页邮箱
- Jsoup简介——使用Java抓取网页数据
- JAVA 抓取网页之GET方法
- Java抓取网页数据(原网页+Javascript返回数据)
- Java抓取网页数据(原网页+Javascript返回数据)
- java网页数据抓取源代码(抓取电话和身份证信息为例)
- Java 抓取网页中的内容【持续更新】
- java 抓取 https 网页内容
- 一般网页信息抓取(Java htmlparser)
- Java抓取网页数据(原网页+Javascript返回数据)
- 一个简单的基于java的网页抓取程序
- java代码抓取网页邮箱的实现方法
- beautifulsoup库简单抓取网页--获取所有链接例子
- java抓取网页乱码问题的处理