当前位置: 移动技术网 > IT编程>开发语言>Java > JAVA使用爬虫抓取网站网页内容的方法

JAVA使用爬虫抓取网站网页内容的方法

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文实例讲述了java使用爬虫抓取网站网页内容的方法。分享给大家供大家参考。具体如下:

最近在用java研究下爬网技术,呵呵,入了个门,把自己的心得和大家分享下
以下提供二种方法,一种是用apache提供的包.另一种是用java自带的.

代码如下:

// 第一种方法
//这种方法是用apache提供的包,简单方便
//但是要用到以下包:commons-codec-1.4.jar
// commons-httpclient-3.1.jar
// commons-logging-1.0.4.jar
public static string createhttpclient(string url, string param) {
  httpclient client = new httpclient();
  string response = null;
  string keyword = null;
  postmethod postmethod = new postmethod(url);
//  try {
//   if (param != null)
//    keyword = new string(param.getbytes("gb2312"), "iso-8859-1");
//  } catch (unsupportedencodingexception e1) {
//   // todo auto-generated catch block
//   e1.printstacktrace();
//  }
  // namevaluepair[] data = { new namevaluepair("keyword", keyword) };
  // // 将表单的值放入postmethod中
  // postmethod.setrequestbody(data);
  // 以上部分是带参数抓取,我自己把它注销了.大家可以把注销消掉研究下
  try {
   int statuscode = client.executemethod(postmethod);
   response = new string(postmethod.getresponsebodyasstring()
     .getbytes("iso-8859-1"), "gb2312");
     //这里要注意下 gb2312要和你抓取网页的编码要一样
   string p = response.replaceall("//&[a-za-z]{1,10};", "")
     .replaceall("<[^>]*>", "");//去掉网页中带有html语言的标签
   system.out.println(p);
  } catch (exception e) {
   e.printstacktrace();
  }
  return response;
}
// 第二种方法
// 这种方法是java自带的url来抓取网站内容
public string getpagecontent(string strurl, string strpostrequest,
   int maxlength) {
  // 读取结果网页
  stringbuffer buffer = new stringbuffer();
  system.setproperty("sun.net.client.defaultconnecttimeout", "5000");
  system.setproperty("sun.net.client.defaultreadtimeout", "5000");
  try {
   url newurl = new url(strurl);
   httpurlconnection hconnect = (httpurlconnection) newurl
     .openconnection();
   // post方式的额外数据
   if (strpostrequest.length() > 0) {
    hconnect.setdooutput(true);
    outputstreamwriter out = new outputstreamwriter(hconnect
      .getoutputstream());
    out.write(strpostrequest);
    out.flush();
    out.close();
   }
   // 读取内容
   bufferedreader rd = new bufferedreader(new inputstreamreader(
     hconnect.getinputstream()));
   int ch;
   for (int length = 0; (ch = rd.read()) > -1
     && (maxlength <= 0 || length < maxlength); length++)
    buffer.append((char) ch);
   string s = buffer.tostring();
   s.replaceall("//&[a-za-z]{1,10};", "").replaceall("<[^>]*>", "");
   system.out.println(s);
   rd.close();
   hconnect.disconnect();
   return buffer.tostring().trim();
  } catch (exception e) {
   // return "错误:读取网页失败!";
   //
   return null;
  }
}

然后写个测试类:

public static void main(string[] args) {
  string url = "//www.jb51.net";
  string keyword = "移动技术网";
  createhttpclient p = new createhttpclient();
  string response = p.createhttpclient(url, keyword);
  // 第一种方法
  // p.getpagecontent(url, "post", 100500);//第二种方法
}

呵呵,看看控制台吧,是不是把网页的内容获取了

希望本文所述对大家的java程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网