java版模拟浏览器下载百度动漫图片到本地。

  1 package javaNet.Instance.ImageDownload;
  2
  3 import java.io.BufferedReader;
  4 import java.io.File;
  5 import java.io.FileOutputStream;
  6 import java.io.IOException;
  7 import java.io.InputStream;
  8 import java.io.InputStreamReader;
  9 import java.net.MalformedURLException;
 10 import java.net.URL;
 11 import java.util.ArrayList;
 12 import java.util.regex.Matcher;
 13 import java.util.regex.Pattern;
 14
 15 public class DownloadImgs {
 16
 17     private String url=null;
 18
 19     public DownloadImgs(String url) {
 20         this.url=url;
 21     }
 22
 23     //----------------------------------gethtml start-----------------------------
 24     /**
 25      * visit the baidu.img page to get the html
 26      * @return inputStream
 27      * @throws IOException
 28      * @throws MalformedURLException
 29      */
 30     public InputStream GetBaiduImgHtml_Stream() throws IOException,MalformedURLException {
 31         URL img_Url=new URL(url);
 32         return img_Url.openStream();
 33     }
 34
 35     /**
 36      * convert the stream to the string
 37      * @param inStrm
 38      * @return string of the page
 39      */
 40     public String InputStreamToString(InputStream inStrm){
 41         BufferedReader reader=new BufferedReader(new InputStreamReader(inStrm));
 42         StringBuilder sb=new StringBuilder();
 43
 44         String  line=null;
 45
 46         try {
 47             while((line=reader.readLine())!=null){
 48                 sb.append(line+‘\n‘);
 49               }
 50             }
 51         catch (IOException e)
 52         {
 53             e.printStackTrace();
 54         }
 55         finally
 56         {
 57             try
 58             {
 59                 inStrm.close();
 60             }
 61             catch (IOException e)
 62             {
 63                 e.printStackTrace();
 64             }
 65         }
 66         return sb.toString();
 67     }
 68
 69     /**
 70      * get the origin page of baidu.img
 71      * @return
 72      * @throws MalformedURLException
 73      * @throws IOException
 74      */
 75     public String GetBaiduImgHtml_Page() throws MalformedURLException, IOException
 76     {
 77         return this.InputStreamToString(this.GetBaiduImgHtml_Stream());
 78     }
 79     /**
 80      * test whether url have been visited the image page,and get the page.
 81      * @param page
 82      */
 83     public void Display_HtmlPage(String page)
 84     {
 85         System.out.println(page);
 86     }
 87     //-------------------------gethtml end----------------
 88     //-------------------------paretoimgurllist start-----
 89     public ArrayList<String> ParsePageToImgList(String page,String imgPa)
 90     {
 91         ArrayList<String> imgList=new ArrayList<String>();
 92         Pattern pattern=Pattern.compile(imgPa);
 93         Matcher matcher=pattern.matcher(page);
 94         while(matcher.find())
 95         {
 96             imgList.add(matcher.group(1));
 97         }
 98         return imgList;
 99     }
100     //------------------------paretoimgurllist end---------
101     //------------------------DownloadFile  start----------
102     public  boolean DownloadFile(String imgUrl,int index,String path)
103     {
104         try
105         {
106             File f=new File(path+"\\"+index+".jpg");
107             System.out.println("下载:"+imgUrl);
108             URL url=new URL(imgUrl);
109             InputStream ins=url.openStream();
110             FileOutputStream fout=new FileOutputStream(f);
111             byte[] buffer=new byte[2048];
112             int bytes_number;
113             while((bytes_number=ins.read(buffer))!=-1)
114             {
115                 fout.write(buffer,0,bytes_number);
116                 fout.flush();
117             }
118             ins.close();
119             fout.close();
120         }
121         catch(Exception e)
122         {
123             System.out.println("下载失败!");
124             e.printStackTrace();
125             return false;
126         }
127         System.out.println("下载完成...");
128         return true;
129     }
130     //------------------------DownloadFile  end----------
131
132     //------------------------mkDir  start----------
133     /**
134      * make a direction for download the images in the native disk.
135      * @param path the native path
136      * @return is success
137      */
138     public void MkDir(String path)
139     {
140         File dir=new File(path);
141         if(!dir.exists())
142         {
143             dir.mkdirs();
144         }
145     }
146     //------------------------mkDir  end------------
147
148     public void Display_ArrayList(ArrayList<String> list)
149     {
150         for(String temp:list)
151         {
152             System.out.println(temp);
153         }
154     }
155
156     public static void main(String[] args) throws MalformedURLException, IOException
157     {
158         String imgPa="\"objURL\":\"(.*?)\"";
159         String path="F:\\photos";
160         int index=0;
161         DownloadImgs downloadimgs=new DownloadImgs("http://image.baidu.com/search/index?"
162                 + "tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=index&fr=&sf=1"
163                 + "&fmq=&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0"
164                 + "&istype=2&ie=utf-8&word=%E5%8A%A8%E6%BC%AB&oq=%E5%8A%A8%E6%BC%AB&rsp=-1");
165         //downloadimgs.Display_HtmlPage(downloadimgs.GetBaiduImgHtml_Page());
166         String htmlPage=downloadimgs.GetBaiduImgHtml_Page();
167         ArrayList<String> imgList=downloadimgs.ParsePageToImgList(htmlPage, imgPa);
168         //downloadimgs.Display_ArrayList(imgList);
169         downloadimgs.MkDir(path);
170         for(String imgUrl:imgList)
171             downloadimgs.DownloadFile(imgUrl, (index++)+1, path);
172
173         System.out.println("一共下载了"+index+"个图片。");
174     }
175 }
时间: 2024-08-09 10:25:37

java版模拟浏览器下载百度动漫图片到本地。的相关文章

java程序模拟浏览器访问Web服务器的处理过程

import java.net.*; import java.io.*; /* * 演示浏览器访问Web服务器的处理过程 */ public class WebServerDemo { public static void main(String[] args)throws IOException{ ServerSocket ss=new ServerSocket(10000); Socket s=ss.accept(); byte[] bytes=new byte[1024]; int len

java 实现模拟浏览器 访问网站

一般的情况下我们都是使用IE或者Navigator浏览器来访问一个WEB服务器,用来浏览页面查看信息或者提交一些数据等等.所访问的这些页面 有的仅仅是一些普通的页面,有的需要用户登录后方可使用,或者需要认证以及是一些通过加密方式传输,例如HTTPS.目前我们使用的浏览器处理这些情况都 不会构成问题.不过你可能在某些时候需要通过程序来访问这样的一些页面,比如从别人的网页中“偷”一些数据:利用某些站点提供的页面来完成某种功能,例如 说我们想知道某个手机号码的归属地而我们自己又没有这样的数据,因此只好

Ueditor结合七牛云及百度云存储(JAVA版,ueditor-1.4.3)实现图片文件上传

[前言] 之前研究了ueditor直接上传图片文件到七牛云以及百度云存储,见下面两篇文章: http://uikoo9.com/blog/detail/ueditor-for-bcs http://uikoo9.com/blog/detail/ueditor-for-qiniu 另外还有一篇ueditor-1.4.3-jsp的使用教程: http://uikoo9.com/blog/detail/how-to-use-ueditor 今天实现了ueditor可配置选择上传到七牛还是百度还是本地,

爬虫下载百度贴吧图片

本次爬取的贴吧是百度的美女吧,给广大男同胞们一些激励 在爬取之前需要在浏览器先登录百度贴吧的帐号,各位也可以在代码中使用post提交或者加入cookie 爬行地址:http://tieba.baidu.com/f?kw=%E7%BE%8E%E5%A5%B3&ie=utf-8&pn=0 #-*- coding:utf-8 -*-import urllib2import reimport requestsfrom lxml import etree 这些是要导入的库,代码并没有使用正则,使用的

下载网页中的图片到本地

简单的一个下载如下 : string url = "http://avatar.csdn.net/A/2/6/2_yefengzhixia.jpg"; string filepath = "D:\\pic.jpg"; WebClient mywebclient = new WebClient(); mywebclient.DownloadFile(url, filepath); MessageBox.Show("OK"); 下面演示一个从网站中下

批量下载百度贴吧帖子图片

总体功能:下载百度贴吧网页的图片 这个例子延续了上一个抓取贴吧楼主发布内容的例子,上一个例子是把图片剔除掉了,这边重新做了一个下载图片的demo,比较简单. 代码: # -*- encoding:utf-8 -*- # 下载贴吧的图片 import re import urllib import urllib2 #下载百度贴吧图片类 class DownloadImg: def getImage(self,Url): request = urllib2.Request(Url) response

【java项目实践】详解Ajax工作原理以及实现异步验证用户名是否存在+源码下载(java版)

一年前,从不知道Ajax是什么,伴随着不断的积累,到现在经常使用,逐渐有了深入的认识.今天,如果想开发一个更加人性化,友好,无刷新,交互性更强的网页,那您的目标一定是Ajax. 介绍 在详细讨论Ajax是什么之前,先让我们花一分钟了解一下Ajax做什么.如图所示: 如上图展示给我们的就是使用Ajax技术实现的效果.伴随着web应用的越来越强大而出现的是等待,等待服务器响应,等待浏览器刷新,等待请求返回和生成新的页面成为了程序员们的最最头疼的难题.随着Ajax的出现使web应用程序变得更完善,更友

【java项目实践】具体解释Ajax工作原理以及实现异步验证username是否存在+源代码下载(java版)

一年前,从不知道Ajax是什么,伴随着不断的积累,到如今常常使用,逐渐有了深入的认识. 今天,假设想开发一个更加人性化,友好,无刷新,交互性更强的网页,那您的目标一定是Ajax. 介绍 在具体讨论Ajax是什么之前,先让我们花一分钟了解一下Ajax做什么.如图所看到的: 如上图展示给我们的就是使用Ajax技术实现的效果.伴随着web应用的越来越强大而出现的是等待.等待server响应,等待浏览器刷新.等待请求返回和生成新的页面成为了程序猿们的最最头疼的难题.随着Ajax的出现使web应用程序变得

java如果模拟请求重启路由器(网络爬虫常用),还有java如何下载图片

我们如果在公司或家里使用网络爬虫去抓取自己索要的一些数据的时候,常常对方的网站有defence机制,会给你的http请求返回500错误,只要是相同IP就请求不到数据,这时候我们只能去重启路由器,这样IP地址会改变,网络爬虫就能正常工作了 下面是通过发送Socket请求来模拟路由器的重启指令: protected void rebotadsl() { try { BufferedOutputStream sender = null; String url = baseURL; URL target