http编程系列(一)——URL使用和爬取博客图片小DEMO

  • 2019 年 10 月 30 日
  • 笔记

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/luo4105/article/details/72486512

URL简介和基本使用

浏览网页基本都用的http协议,java中也可以进行请求http服务器,并获得服务器响应。Java实现网络爬虫、抢购是用http进行。这里接收通过URL去访问http协议服务器,下图是网络请求流程。

简单例子

String urlStr = "http://127.0.0.1:8080/study_ssmvc/restful?id=1&name=2";  URL url = new URL(urlStr);  URLConnection conn= url.openConnection();  InputStream is = conn.getInputStream();  is.close();

这是一个简单的访问代码,其中,openConnection()是打开连接,getInputStream()作用是发送请求并收到响应,响应是InputStream。

这里写一个post带参请求

String urlStr = "http://127.0.0.1:8080/study_ssmvc/restful";  URL url = new URL(urlStr);  HttpURLConnection conn = (HttpURLConnection)url.openConnection();  conn.setRequestMethod("POST");  conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");  conn.setConnectTimeout(60*1000);  conn.setReadTimeout(60*1000);  conn.setDoOutput(true);  conn.setDoInput(true);  String param = "id=1&name=2";  conn.getOutputStream().write(param.getBytes());  System.out.println("发送请求前");  InputStream is = conn.getInputStream();

这里连接类是HttpURLConnection,继承URLConnection。HttpURLConnection可以设置请求类型,请求header中的属性。

同时这里要说一下OutputStream和InputSteam,

在这里OutputStream是输出流,这里的作用是设置参数。

InputSteam是输入流,这里是收到响应。

当代码执行到conn.getInputStream()时,它会发出http请求,并接收响应。

URL不仅可以连接网页,也可以连接http服务器上的图片、视频、文件,并通过InputStream去接收它们。

下载csdn博客图片DEMO

小demo,根据网址,下载某篇csdn博客上所有的图片

思路如下,

1.通过URLConnection获得响应网页代码

2.通过正则表达式得到所有匹配的图片链接,据我观察,csdn博客图片都是” https://img-blog.csdn.net/…/gravity/Center”这类格式。

3.遍历图片链接,下载图片

实现

@Test  public voiddownloadPic() throwsIOException {      URLurl=new URL("http://blog.csdn.net/luo4105/article/details/72326621");     URLConnection urlConnection = url.openConnection();     urlConnection.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 5.0;Windows NT; DigExt)");     InputStream is= urlConnection.getInputStream();     StringBuffer bf= newStringBuffer();     byte[]bytes=new byte[1024];     intc;     while((c = is.read(bytes)) != -1) {         String s = new String(bytes, 0, c, "UTF-8");         bf.append(s);     }     String imgPattern = "https://img-blog.csdn.net.*Center";     Pattern pattern= Pattern.compile(imgPattern);     Matcher matcher= pattern.matcher(bf);     while(matcher.find()) {         String e=matcher.group(0);         System.out.println(e);         URL urlPic=new URL(e);         System.out.println(urlPic.getPath().substring(1));         URLConnection urlPicConn = urlPic.openConnection();         urlPicConn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 5.0;Windows NT; DigExt)");         InputStream isPic = urlPicConn.getInputStream();         FileOutputStream fos = new FileOutputStream("spiders/"+ urlPic.getPath().substring(1)+ ".jpg");         byte[] picbytes = newbyte[1024];          int r;          while((r = isPic.read(picbytes)) != -1) {              fos.write(picbytes, 0, r);          }          fos.close();          isPic.close();     }     is.close();  }

图片下载

代码地址:https://code.csdn.net/luo4105/study_http/tree/master/src/com/lc/https/CsdnPicSpider.java

参考资料

1.http://ifeve.com/java-networking/