jsoup httpclient 爬取网页并下载google图标



jsoup下载地址 http://www.jsoup.org

httpclient下载地址 http://hc.apache.org/downloads.cgi

其他jar包见附件

Java代码   收藏代码
  1. package jsoup;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.util.HashMap;  
  8. import java.util.Map;  
  9.   
  10. import org.apache.commons.io.FileUtils;  
  11. import org.apache.commons.io.IOUtils;  
  12. import org.apache.http.HttpEntity;  
  13. import org.apache.http.HttpResponse;  
  14. import org.apache.http.HttpStatus;  
  15. import org.apache.http.client.methods.HttpGet;  
  16. import org.apache.http.impl.client.DefaultHttpClient;  
  17. import org.apache.http.params.HttpProtocolParams;  
  18. import org.apache.http.util.EntityUtils;  
  19.   
  20. import com.google.api.translate.Language;  
  21. import com.google.api.translate.Translate;  
  22.   
  23. /** 
  24.  * google logo 下载程序 
  25.  */  
  26. public abstract class Crawler {  
  27.   
  28.     /** 
  29.      * 使用google 翻译api 
  30.      *  
  31.      * @param en 
  32.      * @return 
  33.      */  
  34.     public String translateEnToCinese(String en) {  
  35.         Translate.setHttpReferrer("http://www.xxx.com");  
  36.         try {  
  37.             return Translate.execute(en, Language.ENGLISH, Language.CHINESE);  
  38.         } catch (Exception e) {  
  39.             e.printStackTrace();  
  40.         }  
  41.         return "";  
  42.     }  
  43.   
  44.     /** 
  45.      * 获取一个Map 
  46.      *  
  47.      * @return 
  48.      */  
  49.     public Map<String, Object> getMap() {  
  50.         return new HashMap<String, Object>(0);  
  51.     }  
  52.   
  53.     /** 
  54.      * 下载文件 
  55.      *  
  56.      * @param url 
  57.      *            文件http地址 
  58.      * @param dir 
  59.      *            目标文件 
  60.      * @throws IOException 
  61.      */  
  62.     public void downloadFile(String url, String dir) throws Exception {  
  63.         DefaultHttpClient httpClient = new DefaultHttpClient();  
  64.         HttpProtocolParams.setUserAgent(httpClient.getParams(),  
  65.                         "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9");  
  66.         HttpGet httpGet = new HttpGet();  
  67.         httpGet.setURI(new java.net.URI(url));  
  68.           
  69.         InputStream input = null;  
  70.         FileOutputStream output = null;  
  71.         try {  
  72.             HttpResponse response = httpClient.execute(httpGet);  
  73.             HttpEntity entity = response.getEntity();  
  74.             input = entity.getContent();  
  75.             File file = new File(dir);  
  76.             output = FileUtils.openOutputStream(file);  
  77.             IOUtils.copy(input, output);  
  78.         } catch (Exception e){  
  79.             e.printStackTrace();  
  80.         } finally {  
  81.             IOUtils.closeQuietly(output);  
  82.             IOUtils.closeQuietly(input);  
  83.         }  
  84.     }  
  85.   
  86.     /** 
  87.      * 处理GET请求,返回整个页面 
  88.      *  
  89.      * @param url 
  90.      *            访问地址 
  91.      * @param params 
  92.      *            编码参数 
  93.      * @return 
  94.      * @throws Exception 
  95.      */  
  96.     public synchronized String doGet(String url, String... params)  
  97.             throws Exception {  
  98.         DefaultHttpClient httpClient = new DefaultHttpClient(); // 创建httpClient实例  
  99.         HttpProtocolParams.setUserAgent(httpClient.getParams(),  
  100.                         "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9");  
  101.         String charset = "UTF-8";  
  102.         if (null != params && params.length >= 1) {  
  103.             charset = params[0];  
  104.         }  
  105.         HttpGet httpGet = new HttpGet(); // 创建get方法实例  
  106.         String content = "";  
  107.         httpGet.setURI(new java.net.URI(url));  
  108.         try {  
  109.             HttpResponse response = httpClient.execute(httpGet); // 执行请求,得到response对象  
  110.             int resStatu = response.getStatusLine().getStatusCode(); // 得到返回的状态码  
  111.             if (resStatu == HttpStatus.SC_OK) { // 200正常  
  112.                 HttpEntity entity = response.getEntity(); // 获得相应的实体  
  113.                 if (entity != null) {  
  114.                     // 使用EntityUtils的toString方法,传递默认编码,在EntityUtils中的默认编码是ISO-8859-1  
  115.                     content = EntityUtils.toString(entity, charset);  
  116.                 }  
  117.             }  
  118.         } catch (Exception e) {  
  119.             System.out.println("访问【" + url + "】出现异常!");  
  120.             e.printStackTrace();  
  121.         } finally {  
  122.             // 关闭资源  
  123.             httpGet.abort();  
  124.             httpClient.getConnectionManager().shutdown();  
  125.         }  
  126.         return content;  
  127.     }  
  128. }  

 

 

Java代码   收藏代码
  1. package jsoup;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.util.ArrayList;  
  6. import java.util.Date;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9.   
  10. import org.apache.commons.io.FileUtils;  
  11. import org.apache.commons.lang.StringUtils;  
  12. import org.json.JSONArray;  
  13. import org.json.JSONObject;  
  14. import org.jsoup.Jsoup;  
  15. import org.jsoup.nodes.Document;  
  16. import org.jsoup.nodes.Element;  
  17. import org.jsoup.select.Elements;  
  18.   
  19. /** 
  20.  * google logo 下载程序 
  21.  */  
  22. public class GoogleLogoCrawler extends Crawler {  
  23.       
  24.     private static final String URL = "http://www.logocollect.com/google/year.php?key=%y&page=%p";   
  25.   
  26.     private static final String LOGO_URL = "http://www.logocollect.com/google/";  
  27.   
  28.     private static final String[] YEARS = new String[] {   
  29.             //"1998", "1999", "2000",  
  30.             //"2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008",   
  31.             "2009""2010""2011""2012" };  
  32.   
  33.     private static final String INDEX = "http://www.logocollect.com/google/year.php?key=%y";   
  34.   
  35.     private static final String DIR_PATH = "D:\\googlelogos\\";  
  36.   
  37.     public void doStart() {  
  38.         JSONArray array = new JSONArray();  
  39.         for (String year : YEARS) {  
  40.             String ind = INDEX.replaceAll("%y", year);  
  41.             int pageCount = getPageCount(ind);  
  42.             for (int i = 1; i < pageCount+1; i++) {  
  43.                 String url = URL.replaceAll("%y", year).replaceAll("%p", i + "");  
  44.                 String path = year + "_" + i;  
  45.                 start(url, array, DIR_PATH + path + "\\", path);  
  46.             }  
  47.         }  
  48.         try {  
  49.             FileUtils.writeStringToFile(new File(DIR_PATH + "json"), array.toString(), "UTF-8");  
  50.         } catch (IOException e) {  
  51.             e.printStackTrace();  
  52.         }  
  53.         System.out.println(array);  
  54.     }  
  55.       
  56.     public int getPageCount(String url) {  
  57.         int pageCount = 1;  
  58.         try {  
  59.             org.jsoup.nodes.Document doc = Jsoup.connect(url).get();  
  60.               
  61.             String els = doc.html().toString();  
  62.             int start = els.indexOf("总页数") + 4;  
  63.             String temp = els.substring(start);  
  64.             int end = temp.indexOf(",");  
  65.             pageCount = Integer.parseInt(els.substring(start,start+end));  
  66.             System.out.println(pageCount);  
  67.         } catch (IOException e) {  
  68.             e.printStackTrace();  
  69.         }  
  70.         return pageCount;  
  71.     }  
  72.   
  73.     public void start(String url, JSONArray array, String dir, String path) {  
  74.         try {  
  75.             String content = super.doGet(url);  
  76.             Document doc = Jsoup.parse(content);  
  77.             Elements dds = doc.select(".img img");  
  78.             List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(0);  
  79.             for (int i = 0; i < dds.size(); i++) {  
  80.                 Element img = dds.get(i);  
  81.                 String src = img.select("img").first().attr("src");  
  82.                 String title = img.select("img").first().attr("title");  
  83.                 Map<String, Object> map = super.getMap();  
  84.                   
  85.                 map.put("url", LOGO_URL + src);  
  86.                 map.put("title", title);  
  87.                   
  88.                 list.add(map);  
  89.             }  
  90.             JSONArray tempJsonArray = new JSONArray();  
  91.             for (Map<String, Object> map : list) {  
  92.                 JSONObject jsonObject = new JSONObject();  
  93.                 String proxy = StringUtils.substringAfterLast(map.get("url")  
  94.                         .toString(), ".");  
  95.                 long date = new Date().getTime();  
  96.                 String name = date + "." + proxy;  
  97.                 jsonObject.put("url", map.get("url").toString());  
  98.                 jsonObject.put("dir", name);  
  99.                 jsonObject.put("title", map.get("title").toString());  
  100.                   
  101.                 // 翻译  
  102. //              String dateZh = super.translateEnToCinese(map.get("date")  
  103. //                      .toString());  
  104. //              String titleZh = super.translateEnToCinese(map.get("title")  
  105. //                      .toString());  
  106. //              json.put("title_zh_cn", dateZh + " - " + titleZh);  
  107.                   
  108.                 // 下载图片  
  109.                 super.downloadFile(map.get("url").toString(), dir + name);  
  110.                 tempJsonArray.put(jsonObject);  
  111.             }  
  112.             array.put(new JSONObject().put(path, tempJsonArray));  
  113.         } catch (Exception e) {  
  114.             e.printStackTrace();  
  115.         }  
  116.     }  
  117.   
  118.     public static void main(String[] args) throws Exception {  
  119.         new GoogleLogoCrawler().doStart();  
  120.     }  
  121.   
  122. }  
jsoup httpclient 

jsoup下载地址 http://www.jsoup.org

httpclient下载地址 http://hc.apache.org/downloads.cgi

其他jar包见附件

Java代码   收藏代码
  1. package jsoup;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.util.HashMap;  
  8. import java.util.Map;  
  9.   
  10. import org.apache.commons.io.FileUtils;  
  11. import org.apache.commons.io.IOUtils;  
  12. import org.apache.http.HttpEntity;  
  13. import org.apache.http.HttpResponse;  
  14. import org.apache.http.HttpStatus;  
  15. import org.apache.http.client.methods.HttpGet;  
  16. import org.apache.http.impl.client.DefaultHttpClient;  
  17. import org.apache.http.params.HttpProtocolParams;  
  18. import org.apache.http.util.EntityUtils;  
  19.   
  20. import com.google.api.translate.Language;  
  21. import com.google.api.translate.Translate;  
  22.   
  23. /** 
  24.  * google logo 下载程序 
  25.  */  
  26. public abstract class Crawler {  
  27.   
  28.     /** 
  29.      * 使用google 翻译api 
  30.      *  
  31.      * @param en 
  32.      * @return 
  33.      */  
  34.     public String translateEnToCinese(String en) {  
  35.         Translate.setHttpReferrer("http://www.xxx.com");  
  36.         try {  
  37.             return Translate.execute(en, Language.ENGLISH, Language.CHINESE);  
  38.         } catch (Exception e) {  
  39.             e.printStackTrace();  
  40.         }  
  41.         return "";  
  42.     }  
  43.   
  44.     /** 
  45.      * 获取一个Map 
  46.      *  
  47.      * @return 
  48.      */  
  49.     public Map<String, Object> getMap() {  
  50.         return new HashMap<String, Object>(0);  
  51.     }  
  52.   
  53.     /** 
  54.      * 下载文件 
  55.      *  
  56.      * @param url 
  57.      *            文件http地址 
  58.      * @param dir 
  59.      *            目标文件 
  60.      * @throws IOException 
  61.      */  
  62.     public void downloadFile(String url, String dir) throws Exception {  
  63.         DefaultHttpClient httpClient = new DefaultHttpClient();  
  64.         HttpProtocolParams.setUserAgent(httpClient.getParams(),  
  65.                         "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9");  
  66.         HttpGet httpGet = new HttpGet();  
  67.         httpGet.setURI(new java.net.URI(url));  
  68.           
  69.         InputStream input = null;  
  70.         FileOutputStream output = null;  
  71.         try {  
  72.             HttpResponse response = httpClient.execute(httpGet);  
  73.             HttpEntity entity = response.getEntity();  
  74.             input = entity.getContent();  
  75.             File file = new File(dir);  
  76.             output = FileUtils.openOutputStream(file);  
  77.             IOUtils.copy(input, output);  
  78.         } catch (Exception e){  
  79.             e.printStackTrace();  
  80.         } finally {  
  81.             IOUtils.closeQuietly(output);  
  82.             IOUtils.closeQuietly(input);  
  83.         }  
  84.     }  
  85.   
  86.     /** 
  87.      * 处理GET请求,返回整个页面 
  88.      *  
  89.      * @param url 
  90.      *            访问地址 
  91.      * @param params 
  92.      *            编码参数 
  93.      * @return 
  94.      * @throws Exception 
  95.      */  
  96.     public synchronized String doGet(String url, String... params)  
  97.             throws Exception {  
  98.         DefaultHttpClient httpClient = new DefaultHttpClient(); // 创建httpClient实例  
  99.         HttpProtocolParams.setUserAgent(httpClient.getParams(),  
  100.                         "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9");  
  101.         String charset = "UTF-8";  
  102.         if (null != params && params.length >= 1) {  
  103.             charset = params[0];  
  104.         }  
  105.         HttpGet httpGet = new HttpGet(); // 创建get方法实例  
  106.         String content = "";  
  107.         httpGet.setURI(new java.net.URI(url));  
  108.         try {  
  109.             HttpResponse response = httpClient.execute(httpGet); // 执行请求,得到response对象  
  110.             int resStatu = response.getStatusLine().getStatusCode(); // 得到返回的状态码  
  111.             if (resStatu == HttpStatus.SC_OK) { // 200正常  
  112.                 HttpEntity entity = response.getEntity(); // 获得相应的实体  
  113.                 if (entity != null) {  
  114.                     // 使用EntityUtils的toString方法,传递默认编码,在EntityUtils中的默认编码是ISO-8859-1  
  115.                     content = EntityUtils.toString(entity, charset);  
  116.                 }  
  117.             }  
  118.         } catch (Exception e) {  
  119.             System.out.println("访问【" + url + "】出现异常!");  
  120.             e.printStackTrace();  
  121.         } finally {  
  122.             // 关闭资源  
  123.             httpGet.abort();  
  124.             httpClient.getConnectionManager().shutdown();  
  125.         }  
  126.         return content;  
  127.     }  
  128. }  

 

 

Java代码   收藏代码
  1. package jsoup;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.util.ArrayList;  
  6. import java.util.Date;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9.   
  10. import org.apache.commons.io.FileUtils;  
  11. import org.apache.commons.lang.StringUtils;  
  12. import org.json.JSONArray;  
  13. import org.json.JSONObject;  
  14. import org.jsoup.Jsoup;  
  15. import org.jsoup.nodes.Document;  
  16. import org.jsoup.nodes.Element;  
  17. import org.jsoup.select.Elements;  
  18.   
  19. /** 
  20.  * google logo 下载程序 
  21.  */  
  22. public class GoogleLogoCrawler extends Crawler {  
  23.       
  24.     private static final String URL = "http://www.logocollect.com/google/year.php?key=%y&page=%p";   
  25.   
  26.     private static final String LOGO_URL = "http://www.logocollect.com/google/";  
  27.   
  28.     private static final String[] YEARS = new String[] {   
  29.             //"1998", "1999", "2000",  
  30.             //"2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008",   
  31.             "2009""2010""2011""2012" };  
  32.   
  33.     private static final String INDEX = "http://www.logocollect.com/google/year.php?key=%y";   
  34.   
  35.     private static final String DIR_PATH = "D:\\googlelogos\\";  
  36.   
  37.     public void doStart() {  
  38.         JSONArray array = new JSONArray();  
  39.         for (String year : YEARS) {  
  40.             String ind = INDEX.replaceAll("%y", year);  
  41.             int pageCount = getPageCount(ind);  
  42.             for (int i = 1; i < pageCount+1; i++) {  
  43.                 String url = URL.replaceAll("%y", year).replaceAll("%p", i + "");  
  44.                 String path = year + "_" + i;  
  45.                 start(url, array, DIR_PATH + path + "\\", path);  
  46.             }  
  47.         }  
  48.         try {  
  49.             FileUtils.writeStringToFile(new File(DIR_PATH + "json"), array.toString(), "UTF-8");  
  50.         } catch (IOException e) {  
  51.             e.printStackTrace();  
  52.         }  
  53.         System.out.println(array);  
  54.     }  
  55.       
  56.     public int getPageCount(String url) {  
  57.         int pageCount = 1;  
  58.         try {  
  59.             org.jsoup.nodes.Document doc = Jsoup.connect(url).get();  
  60.               
  61.             String els = doc.html().toString();  
  62.             int start = els.indexOf("总页数") + 4;  
  63.             String temp = els.substring(start);  
  64.             int end = temp.indexOf(",");  
  65.             pageCount = Integer.parseInt(els.substring(start,start+end));  
  66.             System.out.println(pageCount);  
  67.         } catch (IOException e) {  
  68.             e.printStackTrace();  
  69.         }  
  70.         return pageCount;  
  71.     }  
  72.   
  73.     public void start(String url, JSONArray array, String dir, String path) {  
  74.         try {  
  75.             String content = super.doGet(url);  
  76.             Document doc = Jsoup.parse(content);  
  77.             Elements dds = doc.select(".img img");  
  78.             List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(0);  
  79.             for (int i = 0; i < dds.size(); i++) {  
  80.                 Element img = dds.get(i);  
  81.                 String src = img.select("img").first().attr("src");  
  82.                 String title = img.select("img").first().attr("title");  
  83.                 Map<String, Object> map = super.getMap();  
  84.                   
  85.                 map.put("url", LOGO_URL + src);  
  86.                 map.put("title", title);  
  87.                   
  88.                 list.add(map);  
  89.             }  
  90.             JSONArray tempJsonArray = new JSONArray();  
  91.             for (Map<String, Object> map : list) {  
  92.                 JSONObject jsonObject = new JSONObject();  
  93.                 String proxy = StringUtils.substringAfterLast(map.get("url")  
  94.                         .toString(), ".");  
  95.                 long date = new Date().getTime();  
  96.                 String name = date + "." + proxy;  
  97.                 jsonObject.put("url", map.get("url").toString());  
  98.                 jsonObject.put("dir", name);  
  99.                 jsonObject.put("title", map.get("title").toString());  
  100.                   
  101.                 // 翻译  
  102. //              String dateZh = super.translateEnToCinese(map.get("date")  
  103. //                      .toString());  
  104. //              String titleZh = super.translateEnToCinese(map.get("title")  
  105. //                      .toString());  
  106. //              json.put("title_zh_cn", dateZh + " - " + titleZh);  
  107.                   
  108.                 // 下载图片  
  109.                 super.downloadFile(map.get("url").toString(), dir + name);  
  110.                 tempJsonArray.put(jsonObject);  
  111.             }  
  112.             array.put(new JSONObject().put(path, tempJsonArray));  
  113.         } catch (Exception e) {  
  114.             e.printStackTrace();  
  115.         }  
  116.     }  
  117.   
  118.     public static void main(String[] args) throws Exception {  
  119.         new GoogleLogoCrawler().doStart();  
  120.     }  
  121.   
  122. }  



jsoup下载地址 http://www.jsoup.org

httpclient下载地址 http://hc.apache.org/downloads.cgi

其他jar包见附件

Java代码   收藏代码
  1. package jsoup;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.util.HashMap;  
  8. import java.util.Map;  
  9.   
  10. import org.apache.commons.io.FileUtils;  
  11. import org.apache.commons.io.IOUtils;  
  12. import org.apache.http.HttpEntity;  
  13. import org.apache.http.HttpResponse;  
  14. import org.apache.http.HttpStatus;  
  15. import org.apache.http.client.methods.HttpGet;  
  16. import org.apache.http.impl.client.DefaultHttpClient;  
  17. import org.apache.http.params.HttpProtocolParams;  
  18. import org.apache.http.util.EntityUtils;  
  19.   
  20. import com.google.api.translate.Language;  
  21. import com.google.api.translate.Translate;  
  22.   
  23. /** 
  24.  * google logo 下载程序 
  25.  */  
  26. public abstract class Crawler {  
  27.   
  28.     /** 
  29.      * 使用google 翻译api 
  30.      *  
  31.      * @param en 
  32.      * @return 
  33.      */  
  34.     public String translateEnToCinese(String en) {  
  35.         Translate.setHttpReferrer("http://www.xxx.com");  
  36.         try {  
  37.             return Translate.execute(en, Language.ENGLISH, Language.CHINESE);  
  38.         } catch (Exception e) {  
  39.             e.printStackTrace();  
  40.         }  
  41.         return "";  
  42.     }  
  43.   
  44.     /** 
  45.      * 获取一个Map 
  46.      *  
  47.      * @return 
  48.      */  
  49.     public Map<String, Object> getMap() {  
  50.         return new HashMap<String, Object>(0);  
  51.     }  
  52.   
  53.     /** 
  54.      * 下载文件 
  55.      *  
  56.      * @param url 
  57.      *            文件http地址 
  58.      * @param dir 
  59.      *            目标文件 
  60.      * @throws IOException 
  61.      */  
  62.     public void downloadFile(String url, String dir) throws Exception {  
  63.         DefaultHttpClient httpClient = new DefaultHttpClient();  
  64.         HttpProtocolParams.setUserAgent(httpClient.getParams(),  
  65.                         "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9");  
  66.         HttpGet httpGet = new HttpGet();  
  67.         httpGet.setURI(new java.net.URI(url));  
  68.           
  69.         InputStream input = null;  
  70.         FileOutputStream output = null;  
  71.         try {  
  72.             HttpResponse response = httpClient.execute(httpGet);  
  73.             HttpEntity entity = response.getEntity();  
  74.             input = entity.getContent();  
  75.             File file = new File(dir);  
  76.             output = FileUtils.openOutputStream(file);  
  77.             IOUtils.copy(input, output);  
  78.         } catch (Exception e){  
  79.             e.printStackTrace();  
  80.         } finally {  
  81.             IOUtils.closeQuietly(output);  
  82.             IOUtils.closeQuietly(input);  
  83.         }  
  84.     }  
  85.   
  86.     /** 
  87.      * 处理GET请求,返回整个页面 
  88.      *  
  89.      * @param url 
  90.      *            访问地址 
  91.      * @param params 
  92.      *            编码参数 
  93.      * @return 
  94.      * @throws Exception 
  95.      */  
  96.     public synchronized String doGet(String url, String... params)  
  97.             throws Exception {  
  98.         DefaultHttpClient httpClient = new DefaultHttpClient(); // 创建httpClient实例  
  99.         HttpProtocolParams.setUserAgent(httpClient.getParams(),  
  100.                         "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9");  
  101.         String charset = "UTF-8";  
  102.         if (null != params && params.length >= 1) {  
  103.             charset = params[0];  
  104.         }  
  105.         HttpGet httpGet = new HttpGet(); // 创建get方法实例  
  106.         String content = "";  
  107.         httpGet.setURI(new java.net.URI(url));  
  108.         try {  
  109.             HttpResponse response = httpClient.execute(httpGet); // 执行请求,得到response对象  
  110.             int resStatu = response.getStatusLine().getStatusCode(); // 得到返回的状态码  
  111.             if (resStatu == HttpStatus.SC_OK) { // 200正常  
  112.                 HttpEntity entity = response.getEntity(); // 获得相应的实体  
  113.                 if (entity != null) {  
  114.                     // 使用EntityUtils的toString方法,传递默认编码,在EntityUtils中的默认编码是ISO-8859-1  
  115.                     content = EntityUtils.toString(entity, charset);  
  116.                 }  
  117.             }  
  118.         } catch (Exception e) {  
  119.             System.out.println("访问【" + url + "】出现异常!");  
  120.             e.printStackTrace();  
  121.         } finally {  
  122.             // 关闭资源  
  123.             httpGet.abort();  
  124.             httpClient.getConnectionManager().shutdown();  
  125.         }  
  126.         return content;  
  127.     }  
  128. }  

 

 

Java代码   收藏代码
  1. package jsoup;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.util.ArrayList;  
  6. import java.util.Date;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9.   
  10. import org.apache.commons.io.FileUtils;  
  11. import org.apache.commons.lang.StringUtils;  
  12. import org.json.JSONArray;  
  13. import org.json.JSONObject;  
  14. import org.jsoup.Jsoup;  
  15. import org.jsoup.nodes.Document;  
  16. import org.jsoup.nodes.Element;  
  17. import org.jsoup.select.Elements;  
  18.   
  19. /** 
  20.  * google logo 下载程序 
  21.  */  
  22. public class GoogleLogoCrawler extends Crawler {  
  23.       
  24.     private static final String URL = "http://www.logocollect.com/google/year.php?key=%y&page=%p";   
  25.   
  26.     private static final String LOGO_URL = "http://www.logocollect.com/google/";  
  27.   
  28.     private static final String[] YEARS = new String[] {   
  29.             //"1998", "1999", "2000",  
  30.             //"2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008",   
  31.             "2009""2010""2011""2012" };  
  32.   
  33.     private static final String INDEX = "http://www.logocollect.com/google/year.php?key=%y";   
  34.   
  35.     private static final String DIR_PATH = "D:\\googlelogos\\";  
  36.   
  37.     public void doStart() {  
  38.         JSONArray array = new JSONArray();  
  39.         for (String year : YEARS) {  
  40.             String ind = INDEX.replaceAll("%y", year);  
  41.             int pageCount = getPageCount(ind);  
  42.             for (int i = 1; i < pageCount+1; i++) {  
  43.                 String url = URL.replaceAll("%y", year).replaceAll("%p", i + "");  
  44.                 String path = year + "_" + i;  
  45.                 start(url, array, DIR_PATH + path + "\\", path);  
  46.             }  
  47.         }  
  48.         try {  
  49.             FileUtils.writeStringToFile(new File(DIR_PATH + "json"), array.toString(), "UTF-8");  
  50.         } catch (IOException e) {  
  51.             e.printStackTrace();  
  52.         }  
  53.         System.out.println(array);  
  54.     }  
  55.       
  56.     public int getPageCount(String url) {  
  57.         int pageCount = 1;  
  58.         try {  
  59.             org.jsoup.nodes.Document doc = Jsoup.connect(url).get();  
  60.               
  61.             String els = doc.html().toString();  
  62.             int start = els.indexOf("总页数") + 4;  
  63.             String temp = els.substring(start);  
  64.             int end = temp.indexOf(",");  
  65.             pageCount = Integer.parseInt(els.substring(start,start+end));  
  66.             System.out.println(pageCount);  
  67.         } catch (IOException e) {  
  68.             e.printStackTrace();  
  69.         }  
  70.         return pageCount;  
  71.     }  
  72.   
  73.     public void start(String url, JSONArray array, String dir, String path) {  
  74.         try {  
  75.             String content = super.doGet(url);  
  76.             Document doc = Jsoup.parse(content);  
  77.             Elements dds = doc.select(".img img");  
  78.             List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(0);  
  79.             for (int i = 0; i < dds.size(); i++) {  
  80.                 Element img = dds.get(i);  
  81.                 String src = img.select("img").first().attr("src");  
  82.                 String title = img.select("img").first().attr("title");  
  83.                 Map<String, Object> map = super.getMap();  
  84.                   
  85.                 map.put("url", LOGO_URL + src);  
  86.                 map.put("title", title);  
  87.                   
  88.                 list.add(map);  
  89.             }  
  90.             JSONArray tempJsonArray = new JSONArray();  
  91.             for (Map<String, Object> map : list) {  
  92.                 JSONObject jsonObject = new JSONObject();  
  93.                 String proxy = StringUtils.substringAfterLast(map.get("url")  
  94.                         .toString(), ".");  
  95.                 long date = new Date().getTime();  
  96.                 String name = date + "." + proxy;  
  97.                 jsonObject.put("url", map.get("url").toString());  
  98.                 jsonObject.put("dir", name);  
  99.                 jsonObject.put("title", map.get("title").toString());  
  100.                   
  101.                 // 翻译  
  102. //              String dateZh = super.translateEnToCinese(map.get("date")  
  103. //                      .toString());  
  104. //              String titleZh = super.translateEnToCinese(map.get("title")  
  105. //                      .toString());  
  106. //              json.put("title_zh_cn", dateZh + " - " + titleZh);  
  107.                   
  108.                 // 下载图片  
  109.                 super.downloadFile(map.get("url").toString(), dir + name);  
  110.                 tempJsonArray.put(jsonObject);  
  111.             }  
  112.             array.put(new JSONObject().put(path, tempJsonArray));  
  113.         } catch (Exception e) {  
  114.             e.printStackTrace();  
  115.         }  
  116.     }  
  117.   
  118.     public static void main(String[] args) throws Exception {  
  119.         new GoogleLogoCrawler().doStart();  
  120.     }  
  121.   
  122. }  


jsoup下载地址 http://www.jsoup.org

httpclient下载地址 http://hc.apache.org/downloads.cgi

其他jar包见附件

Java代码   收藏代码
  1. package jsoup;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.util.HashMap;  
  8. import java.util.Map;  
  9.   
  10. import org.apache.commons.io.FileUtils;  
  11. import org.apache.commons.io.IOUtils;  
  12. import org.apache.http.HttpEntity;  
  13. import org.apache.http.HttpResponse;  
  14. import org.apache.http.HttpStatus;  
  15. import org.apache.http.client.methods.HttpGet;  
  16. import org.apache.http.impl.client.DefaultHttpClient;  
  17. import org.apache.http.params.HttpProtocolParams;  
  18. import org.apache.http.util.EntityUtils;  
  19.   
  20. import com.google.api.translate.Language;  
  21. import com.google.api.translate.Translate;  
  22.   
  23. /** 
  24.  * google logo 下载程序 
  25.  */  
  26. public abstract class Crawler {  
  27.   
  28.     /** 
  29.      * 使用google 翻译api 
  30.      *  
  31.      * @param en 
  32.      * @return 
  33.      */  
  34.     public String translateEnToCinese(String en) {  
  35.         Translate.setHttpReferrer("http://www.xxx.com");  
  36.         try {  
  37.             return Translate.execute(en, Language.ENGLISH, Language.CHINESE);  
  38.         } catch (Exception e) {  
  39.             e.printStackTrace();  
  40.         }  
  41.         return "";  
  42.     }  
  43.   
  44.     /** 
  45.      * 获取一个Map 
  46.      *  
  47.      * @return 
  48.      */  
  49.     public Map<String, Object> getMap() {  
  50.         return new HashMap<String, Object>(0);  
  51.     }  
  52.   
  53.     /** 
  54.      * 下载文件 
  55.      *  
  56.      * @param url 
  57.      *            文件http地址 
  58.      * @param dir 
  59.      *            目标文件 
  60.      * @throws IOException 
  61.      */  
  62.     public void downloadFile(String url, String dir) throws Exception {  
  63.         DefaultHttpClient httpClient = new DefaultHttpClient();  
  64.         HttpProtocolParams.setUserAgent(httpClient.getParams(),  
  65.                         "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9");  
  66.         HttpGet httpGet = new HttpGet();  
  67.         httpGet.setURI(new java.net.URI(url));  
  68.           
  69.         InputStream input = null;  
  70.         FileOutputStream output = null;  
  71.         try {  
  72.             HttpResponse response = httpClient.execute(httpGet);  
  73.             HttpEntity entity = response.getEntity();  
  74.             input = entity.getContent();  
  75.             File file = new File(dir);  
  76.             output = FileUtils.openOutputStream(file);  
  77.             IOUtils.copy(input, output);  
  78.         } catch (Exception e){  
  79.             e.printStackTrace();  
  80.         } finally {  
  81.             IOUtils.closeQuietly(output);  
  82.             IOUtils.closeQuietly(input);  
  83.         }  
  84.     }  
  85.   
  86.     /** 
  87.      * 处理GET请求,返回整个页面 
  88.      *  
  89.      * @param url 
  90.      *            访问地址 
  91.      * @param params 
  92.      *            编码参数 
  93.      * @return 
  94.      * @throws Exception 
  95.      */  
  96.     public synchronized String doGet(String url, String... params)  
  97.             throws Exception {  
  98.         DefaultHttpClient httpClient = new DefaultHttpClient(); // 创建httpClient实例  
  99.         HttpProtocolParams.setUserAgent(httpClient.getParams(),  
  100.                         "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9");  
  101.         String charset = "UTF-8";  
  102.         if (null != params && params.length >= 1) {  
  103.             charset = params[0];  
  104.         }  
  105.         HttpGet httpGet = new HttpGet(); // 创建get方法实例  
  106.         String content = "";  
  107.         httpGet.setURI(new java.net.URI(url));  
  108.         try {  
  109.             HttpResponse response = httpClient.execute(httpGet); // 执行请求,得到response对象  
  110.             int resStatu = response.getStatusLine().getStatusCode(); // 得到返回的状态码  
  111.             if (resStatu == HttpStatus.SC_OK) { // 200正常  
  112.                 HttpEntity entity = response.getEntity(); // 获得相应的实体  
  113.                 if (entity != null) {  
  114.                     // 使用EntityUtils的toString方法,传递默认编码,在EntityUtils中的默认编码是ISO-8859-1  
  115.                     content = EntityUtils.toString(entity, charset);  
  116.                 }  
  117.             }  
  118.         } catch (Exception e) {  
  119.             System.out.println("访问【" + url + "】出现异常!");  
  120.             e.printStackTrace();  
  121.         } finally {  
  122.             // 关闭资源  
  123.             httpGet.abort();  
  124.             httpClient.getConnectionManager().shutdown();  
  125.         }  
  126.         return content;  
  127.     }  
  128. }  

 

 

Java代码   收藏代码
  1. package jsoup;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.util.ArrayList;  
  6. import java.util.Date;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9.   
  10. import org.apache.commons.io.FileUtils;  
  11. import org.apache.commons.lang.StringUtils;  
  12. import org.json.JSONArray;  
  13. import org.json.JSONObject;  
  14. import org.jsoup.Jsoup;  
  15. import org.jsoup.nodes.Document;  
  16. import org.jsoup.nodes.Element;  
  17. import org.jsoup.select.Elements;  
  18.   
  19. /** 
  20.  * google logo 下载程序 
  21.  */  
  22. public class GoogleLogoCrawler extends Crawler {  
  23.       
  24.     private static final String URL = "http://www.logocollect.com/google/year.php?key=%y&page=%p";   
  25.   
  26.     private static final String LOGO_URL = "http://www.logocollect.com/google/";  
  27.   
  28.     private static final String[] YEARS = new String[] {   
  29.             //"1998", "1999", "2000",  
  30.             //"2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008",   
  31.             "2009""2010""2011""2012" };  
  32.   
  33.     private static final String INDEX = "http://www.logocollect.com/google/year.php?key=%y";   
  34.   
  35.     private static final String DIR_PATH = "D:\\googlelogos\\";  
  36.   
  37.     public void doStart() {  
  38.         JSONArray array = new JSONArray();  
  39.         for (String year : YEARS) {  
  40.             String ind = INDEX.replaceAll("%y", year);  
  41.             int pageCount = getPageCount(ind);  
  42.             for (int i = 1; i < pageCount+1; i++) {  
  43.                 String url = URL.replaceAll("%y", year).replaceAll("%p", i + "");  
  44.                 String path = year + "_" + i;  
  45.                 start(url, array, DIR_PATH + path + "\\", path);  
  46.             }  
  47.         }  
  48.         try {  
  49.             FileUtils.writeStringToFile(new File(DIR_PATH + "json"), array.toString(), "UTF-8");  
  50.         } catch (IOException e) {  
  51.             e.printStackTrace();  
  52.         }  
  53.         System.out.println(array);  
  54.     }  
  55.       
  56.     public int getPageCount(String url) {  
  57.         int pageCount = 1;  
  58.         try {  
  59.             org.jsoup.nodes.Document doc = Jsoup.connect(url).get();  
  60.               
  61.             String els = doc.html().toString();  
  62.             int start = els.indexOf("总页数") + 4;  
  63.             String temp = els.substring(start);  
  64.             int end = temp.indexOf(",");  
  65.             pageCount = Integer.parseInt(els.substring(start,start+end));  
  66.             System.out.println(pageCount);  
  67.         } catch (IOException e) {  
  68.             e.printStackTrace();  
  69.         }  
  70.         return pageCount;  
  71.     }  
  72.   
  73.     public void start(String url, JSONArray array, String dir, String path) {  
  74.         try {  
  75.             String content = super.doGet(url);  
  76.             Document doc = Jsoup.parse(content);  
  77.             Elements dds = doc.select(".img img");  
  78.             List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(0);  
  79.             for (int i = 0; i < dds.size(); i++) {  
  80.                 Element img = dds.get(i);  
  81.                 String src = img.select("img").first().attr("src");  
  82.                 String title = img.select("img").first().attr("title");  
  83.                 Map<String, Object> map = super.getMap();  
  84.                   
  85.                 map.put("url", LOGO_URL + src);  
  86.                 map.put("title", title);  
  87.                   
  88.                 list.add(map);  
  89.             }  
  90.             JSONArray tempJsonArray = new JSONArray();  
  91.             for (Map<String, Object> map : list) {  
  92.                 JSONObject jsonObject = new JSONObject();  
  93.                 String proxy = StringUtils.substringAfterLast(map.get("url")  
  94.                         .toString(), ".");  
  95.                 long date = new Date().getTime();  
  96.                 String name = date + "." + proxy;  
  97.                 jsonObject.put("url", map.get("url").toString());  
  98.                 jsonObject.put("dir", name);  
  99.                 jsonObject.put("title", map.get("title").toString());  
  100.                   
  101.                 // 翻译  
  102. //              String dateZh = super.translateEnToCinese(map.get("date")  
  103. //                      .toString());  
  104. //              String titleZh = super.translateEnToCinese(map.get("title")  
  105. //                      .toString());  
  106. //              json.put("title_zh_cn", dateZh + " - " + titleZh);  
  107.                   
  108.                 // 下载图片  
  109.                 super.downloadFile(map.get("url").toString(), dir + name);  
  110.                 tempJsonArray.put(jsonObject);  
  111.             }  
  112.             array.put(new JSONObject().put(path, tempJsonArray));  
  113.         } catch (Exception e) {  
  114.             e.printStackTrace();  
  115.         }  
  116.     }  
  117.   
  118.     public static void main(String[] args) throws Exception {  
  119.         new GoogleLogoCrawler().doStart();  
  120.     }  
  121.   
  122. }  
jsoup httpclient 

jsoup下载地址 http://www.jsoup.org

httpclient下载地址 http://hc.apache.org/downloads.cgi

其他jar包见附件

Java代码   收藏代码
  1. package jsoup;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.util.HashMap;  
  8. import java.util.Map;  
  9.   
  10. import org.apache.commons.io.FileUtils;  
  11. import org.apache.commons.io.IOUtils;  
  12. import org.apache.http.HttpEntity;  
  13. import org.apache.http.HttpResponse;  
  14. import org.apache.http.HttpStatus;  
  15. import org.apache.http.client.methods.HttpGet;  
  16. import org.apache.http.impl.client.DefaultHttpClient;  
  17. import org.apache.http.params.HttpProtocolParams;  
  18. import org.apache.http.util.EntityUtils;  
  19.   
  20. import com.google.api.translate.Language;  
  21. import com.google.api.translate.Translate;  
  22.   
  23. /** 
  24.  * google logo 下载程序 
  25.  */  
  26. public abstract class Crawler {  
  27.   
  28.     /** 
  29.      * 使用google 翻译api 
  30.      *  
  31.      * @param en 
  32.      * @return 
  33.      */  
  34.     public String translateEnToCinese(String en) {  
  35.         Translate.setHttpReferrer("http://www.xxx.com");  
  36.         try {  
  37.             return Translate.execute(en, Language.ENGLISH, Language.CHINESE);  
  38.         } catch (Exception e) {  
  39.             e.printStackTrace();  
  40.         }  
  41.         return "";  
  42.     }  
  43.   
  44.     /** 
  45.      * 获取一个Map 
  46.      *  
  47.      * @return 
  48.      */  
  49.     public Map<String, Object> getMap() {  
  50.         return new HashMap<String, Object>(0);  
  51.     }  
  52.   
  53.     /** 
  54.      * 下载文件 
  55.      *  
  56.      * @param url 
  57.      *            文件http地址 
  58.      * @param dir 
  59.      *            目标文件 
  60.      * @throws IOException 
  61.      */  
  62.     public void downloadFile(String url, String dir) throws Exception {  
  63.         DefaultHttpClient httpClient = new DefaultHttpClient();  
  64.         HttpProtocolParams.setUserAgent(httpClient.getParams(),  
  65.                         "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9");  
  66.         HttpGet httpGet = new HttpGet();  
  67.         httpGet.setURI(new java.net.URI(url));  
  68.           
  69.         InputStream input = null;  
  70.         FileOutputStream output = null;  
  71.         try {  
  72.             HttpResponse response = httpClient.execute(httpGet);  
  73.             HttpEntity entity = response.getEntity();  
  74.             input = entity.getContent();  
  75.             File file = new File(dir);  
  76.             output = FileUtils.openOutputStream(file);  
  77.             IOUtils.copy(input, output);  
  78.         } catch (Exception e){  
  79.             e.printStackTrace();  
  80.         } finally {  
  81.             IOUtils.closeQuietly(output);  
  82.             IOUtils.closeQuietly(input);  
  83.         }  
  84.     }  
  85.   
  86.     /** 
  87.      * 处理GET请求,返回整个页面 
  88.      *  
  89.      * @param url 
  90.      *            访问地址 
  91.      * @param params 
  92.      *            编码参数 
  93.      * @return 
  94.      * @throws Exception 
  95.      */  
  96.     public synchronized String doGet(String url, String... params)  
  97.             throws Exception {  
  98.         DefaultHttpClient httpClient = new DefaultHttpClient(); // 创建httpClient实例  
  99.         HttpProtocolParams.setUserAgent(httpClient.getParams(),  
  100.                         "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9");  
  101.         String charset = "UTF-8";  
  102.         if (null != params && params.length >= 1) {  
  103.             charset = params[0];  
  104.         }  
  105.         HttpGet httpGet = new HttpGet(); // 创建get方法实例  
  106.         String content = "";  
  107.         httpGet.setURI(new java.net.URI(url));  
  108.         try {  
  109.             HttpResponse response = httpClient.execute(httpGet); // 执行请求,得到response对象  
  110.             int resStatu = response.getStatusLine().getStatusCode(); // 得到返回的状态码  
  111.             if (resStatu == HttpStatus.SC_OK) { // 200正常  
  112.                 HttpEntity entity = response.getEntity(); // 获得相应的实体  
  113.                 if (entity != null) {  
  114.                     // 使用EntityUtils的toString方法,传递默认编码,在EntityUtils中的默认编码是ISO-8859-1  
  115.                     content = EntityUtils.toString(entity, charset);  
  116.                 }  
  117.             }  
  118.         } catch (Exception e) {  
  119.             System.out.println("访问【" + url + "】出现异常!");  
  120.             e.printStackTrace();  
  121.         } finally {  
  122.             // 关闭资源  
  123.             httpGet.abort();  
  124.             httpClient.getConnectionManager().shutdown();  
  125.         }  
  126.         return content;  
  127.     }  
  128. }  

 

 

Java代码   收藏代码
  1. package jsoup;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.util.ArrayList;  
  6. import java.util.Date;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9.   
  10. import org.apache.commons.io.FileUtils;  
  11. import org.apache.commons.lang.StringUtils;  
  12. import org.json.JSONArray;  
  13. import org.json.JSONObject;  
  14. import org.jsoup.Jsoup;  
  15. import org.jsoup.nodes.Document;  
  16. import org.jsoup.nodes.Element;  
  17. import org.jsoup.select.Elements;  
  18.   
  19. /** 
  20.  * google logo 下载程序 
  21.  */  
  22. public class GoogleLogoCrawler extends Crawler {  
  23.       
  24.     private static final String URL = "http://www.logocollect.com/google/year.php?key=%y&page=%p";   
  25.   
  26.     private static final String LOGO_URL = "http://www.logocollect.com/google/";  
  27.   
  28.     private static final String[] YEARS = new String[] {   
  29.             //"1998", "1999", "2000",  
  30.             //"2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008",   
  31.             "2009""2010""2011""2012" };  
  32.   
  33.     private static final String INDEX = "http://www.logocollect.com/google/year.php?key=%y";   
  34.   
  35.     private static final String DIR_PATH = "D:\\googlelogos\\";  
  36.   
  37.     public void doStart() {  
  38.         JSONArray array = new JSONArray();  
  39.         for (String year : YEARS) {  
  40.             String ind = INDEX.replaceAll("%y", year);  
  41.             int pageCount = getPageCount(ind);  
  42.             for (int i = 1; i < pageCount+1; i++) {  
  43.                 String url = URL.replaceAll("%y", year).replaceAll("%p", i + "");  
  44.                 String path = year + "_" + i;  
  45.                 start(url, array, DIR_PATH + path + "\\", path);  
  46.             }  
  47.         }  
  48.         try {  
  49.             FileUtils.writeStringToFile(new File(DIR_PATH + "json"), array.toString(), "UTF-8");  
  50.         } catch (IOException e) {  
  51.             e.printStackTrace();  
  52.         }  
  53.         System.out.println(array);  
  54.     }  
  55.       
  56.     public int getPageCount(String url) {  
  57.         int pageCount = 1;  
  58.         try {  
  59.             org.jsoup.nodes.Document doc = Jsoup.connect(url).get();  
  60.               
  61.             String els = doc.html().toString();  
  62.             int start = els.indexOf("总页数") + 4;  
  63.             String temp = els.substring(start);  
  64.             int end = temp.indexOf(",");  
  65.             pageCount = Integer.parseInt(els.substring(start,start+end));  
  66.             System.out.println(pageCount);  
  67.         } catch (IOException e) {  
  68.             e.printStackTrace();  
  69.         }  
  70.         return pageCount;  
  71.     }  
  72.   
  73.     public void start(String url, JSONArray array, String dir, String path) {  
  74.         try {  
  75.             String content = super.doGet(url);  
  76.             Document doc = Jsoup.parse(content);  
  77.             Elements dds = doc.select(".img img");  
  78.             List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(0);  
  79.             for (int i = 0; i < dds.size(); i++) {  
  80.                 Element img = dds.get(i);  
  81.                 String src = img.select("img").first().attr("src");  
  82.                 String title = img.select("img").first().attr("title");  
  83.                 Map<String, Object> map = super.getMap();  
  84.                   
  85.                 map.put("url", LOGO_URL + src);  
  86.                 map.put("title", title);  
  87.                   
  88.                 list.add(map);  
  89.             }  
  90.             JSONArray tempJsonArray = new JSONArray();  
  91.             for (Map<String, Object> map : list) {  
  92.                 JSONObject jsonObject = new JSONObject();  
  93.                 String proxy = StringUtils.substringAfterLast(map.get("url")  
  94.                         .toString(), ".");  
  95.                 long date = new Date().getTime();  
  96.                 String name = date + "." + proxy;  
  97.                 jsonObject.put("url", map.get("url").toString());  
  98.                 jsonObject.put("dir", name);  
  99.                 jsonObject.put("title", map.get("title").toString());  
  100.                   
  101.                 // 翻译  
  102. //              String dateZh = super.translateEnToCinese(map.get("date")  
  103. //                      .toString());  
  104. //              String titleZh = super.translateEnToCinese(map.get("title")  
  105. //                      .toString());  
  106. //              json.put("title_zh_cn", dateZh + " - " + titleZh);  
  107.                   
  108.                 // 下载图片  
  109.                 super.downloadFile(map.get("url").toString(), dir + name);  
  110.                 tempJsonArray.put(jsonObject);  
  111.             }  
  112.             array.put(new JSONObject().put(path, tempJsonArray));  
  113.         } catch (Exception e) {  
  114.             e.printStackTrace();  
  115.         }  
  116.     }  
  117.   
  118.     public static void main(String[] args) throws Exception {  
  119.         new GoogleLogoCrawler().doStart();  
  120.     }  
  121.   
  122. }  

jsoup下载地址 http://www.jsoup.org

httpclient下载地址 http://hc.apache.org/downloads.cgi

其他jar包见附件

Java代码   收藏代码
  1. package jsoup;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.util.HashMap;  
  8. import java.util.Map;  
  9.   
  10. import org.apache.commons.io.FileUtils;  
  11. import org.apache.commons.io.IOUtils;  
  12. import org.apache.http.HttpEntity;  
  13. import org.apache.http.HttpResponse;  
  14. import org.apache.http.HttpStatus;  
  15. import org.apache.http.client.methods.HttpGet;  
  16. import org.apache.http.impl.client.DefaultHttpClient;  
  17. import org.apache.http.params.HttpProtocolParams;  
  18. import org.apache.http.util.EntityUtils;  
  19.   
  20. import com.google.api.translate.Language;  
  21. import com.google.api.translate.Translate;  
  22.   
  23. /** 
  24.  * google logo 下载程序 
  25.  */  
  26. public abstract class Crawler {  
  27.   
  28.     /** 
  29.      * 使用google 翻译api 
  30.      *  
  31.      * @param en 
  32.      * @return 
  33.      */  
  34.     public String translateEnToCinese(String en) {  
  35.         Translate.setHttpReferrer("http://www.xxx.com");  
  36.         try {  
  37.             return Translate.execute(en, Language.ENGLISH, Language.CHINESE);  
  38.         } catch (Exception e) {  
  39.             e.printStackTrace();  
  40.         }  
  41.         return "";  
  42.     }  
  43.   
  44.     /** 
  45.      * 获取一个Map 
  46.      *  
  47.      * @return 
  48.      */  
  49.     public Map<String, Object> getMap() {  
  50.         return new HashMap<String, Object>(0);  
  51.     }  
  52.   
  53.     /** 
  54.      * 下载文件 
  55.      *  
  56.      * @param url 
  57.      *            文件http地址 
  58.      * @param dir 
  59.      *            目标文件 
  60.      * @throws IOException 
  61.      */  
  62.     public void downloadFile(String url, String dir) throws Exception {  
  63.         DefaultHttpClient httpClient = new DefaultHttpClient();  
  64.         HttpProtocolParams.setUserAgent(httpClient.getParams(),  
  65.                         "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9");  
  66.         HttpGet httpGet = new HttpGet();  
  67.         httpGet.setURI(new java.net.URI(url));  
  68.           
  69.         InputStream input = null;  
  70.         FileOutputStream output = null;  
  71.         try {  
  72.             HttpResponse response = httpClient.execute(httpGet);  
  73.             HttpEntity entity = response.getEntity();  
  74.             input = entity.getContent();  
  75.             File file = new File(dir);  
  76.             output = FileUtils.openOutputStream(file);  
  77.             IOUtils.copy(input, output);  
  78.         } catch (Exception e){  
  79.             e.printStackTrace();  
  80.         } finally {  
  81.             IOUtils.closeQuietly(output);  
  82.             IOUtils.closeQuietly(input);  
  83.         }  
  84.     }  
  85.   
  86.     /** 
  87.      * 处理GET请求,返回整个页面 
  88.      *  
  89.      * @param url 
  90.      *            访问地址 
  91.      * @param params 
  92.      *            编码参数 
  93.      * @return 
  94.      * @throws Exception 
  95.      */  
  96.     public synchronized String doGet(String url, String... params)  
  97.             throws Exception {  
  98.         DefaultHttpClient httpClient = new DefaultHttpClient(); // 创建httpClient实例  
  99.         HttpProtocolParams.setUserAgent(httpClient.getParams(),  
  100.                         "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9");  
  101.         String charset = "UTF-8";  
  102.         if (null != params && params.length >= 1) {  
  103.             charset = params[0];  
  104.         }  
  105.         HttpGet httpGet = new HttpGet(); // 创建get方法实例  
  106.         String content = "";  
  107.         httpGet.setURI(new java.net.URI(url));  
  108.         try {  
  109.             HttpResponse response = httpClient.execute(httpGet); // 执行请求,得到response对象  
  110.             int resStatu = response.getStatusLine().getStatusCode(); // 得到返回的状态码  
  111.             if (resStatu == HttpStatus.SC_OK) { // 200正常  
  112.                 HttpEntity entity = response.getEntity(); // 获得相应的实体  
  113.                 if (entity != null) {  
  114.                     // 使用EntityUtils的toString方法,传递默认编码,在EntityUtils中的默认编码是ISO-8859-1  
  115.                     content = EntityUtils.toString(entity, charset);  
  116.                 }  
  117.             }  
  118.         } catch (Exception e) {  
  119.             System.out.println("访问【" + url + "】出现异常!");  
  120.             e.printStackTrace();  
  121.         } finally {  
  122.             // 关闭资源  
  123.             httpGet.abort();  
  124.             httpClient.getConnectionManager().shutdown();  
  125.         }  
  126.         return content;  
  127.     }  
  128. }  

 

 

Java代码   收藏代码
  1. package jsoup;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.util.ArrayList;  
  6. import java.util.Date;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9.   
  10. import org.apache.commons.io.FileUtils;  
  11. import org.apache.commons.lang.StringUtils;  
  12. import org.json.JSONArray;  
  13. import org.json.JSONObject;  
  14. import org.jsoup.Jsoup;  
  15. import org.jsoup.nodes.Document;  
  16. import org.jsoup.nodes.Element;  
  17. import org.jsoup.select.Elements;  
  18.   
  19. /** 
  20.  * google logo 下载程序 
  21.  */  
  22. public class GoogleLogoCrawler extends Crawler {  
  23.       
  24.     private static final String URL = "http://www.logocollect.com/google/year.php?key=%y&page=%p";   
  25.   
  26.     private static final String LOGO_URL = "http://www.logocollect.com/google/";  
  27.   
  28.     private static final String[] YEARS = new String[] {   
  29.             //"1998", "1999", "2000",  
  30.             //"2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008",   
  31.             "2009""2010""2011""2012" };  
  32.   
  33.     private static final String INDEX = "http://www.logocollect.com/google/year.php?key=%y";   
  34.   
  35.     private static final String DIR_PATH = "D:\\googlelogos\\";  
  36.   
  37.     public void doStart() {  
  38.         JSONArray array = new JSONArray();  
  39.         for (String year : YEARS) {  
  40.             String ind = INDEX.replaceAll("%y", year);  
  41.             int pageCount = getPageCount(ind);  
  42.             for (int i = 1; i < pageCount+1; i++) {  
  43.                 String url = URL.replaceAll("%y", year).replaceAll("%p", i + "");  
  44.                 String path = year + "_" + i;  
  45.                 start(url, array, DIR_PATH + path + "\\", path);  
  46.             }  
  47.         }  
  48.         try {  
  49.             FileUtils.writeStringToFile(new File(DIR_PATH + "json"), array.toString(), "UTF-8");  
  50.         } catch (IOException e) {  
  51.             e.printStackTrace();  
  52.         }  
  53.         System.out.println(array);  
  54.     }  
  55.       
  56.     public int getPageCount(String url) {  
  57.         int pageCount = 1;  
  58.         try {  
  59.             org.jsoup.nodes.Document doc = Jsoup.connect(url).get();  
  60.               
  61.             String els = doc.html().toString();  
  62.             int start = els.indexOf("总页数") + 4;  
  63.             String temp = els.substring(start);  
  64.             int end = temp.indexOf(",");  
  65.             pageCount = Integer.parseInt(els.substring(start,start+end));  
  66.             System.out.println(pageCount);  
  67.         } catch (IOException e) {  
  68.             e.printStackTrace();  
  69.         }  
  70.         return pageCount;  
  71.     }  
  72.   
  73.     public void start(String url, JSONArray array, String dir, String path) {  
  74.         try {  
  75.             String content = super.doGet(url);  
  76.             Document doc = Jsoup.parse(content);  
  77.             Elements dds = doc.select(".img img");  
  78.             List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(0);  
  79.             for (int i = 0; i < dds.size(); i++) {  
  80.                 Element img = dds.get(i);  
  81.                 String src = img.select("img").first().attr("src");  
  82.                 String title = img.select("img").first().attr("title");  
  83.                 Map<String, Object> map = super.getMap();  
  84.                   
  85.                 map.put("url", LOGO_URL + src);  
  86.                 map.put("title", title);  
  87.                   
  88.                 list.add(map);  
  89.             }  
  90.             JSONArray tempJsonArray = new JSONArray();  
  91.             for (Map<String, Object> map : list) {  
  92.                 JSONObject jsonObject = new JSONObject();  
  93.                 String proxy = StringUtils.substringAfterLast(map.get("url")  
  94.                         .toString(), ".");  
  95.                 long date = new Date().getTime();  
  96.                 String name = date + "." + proxy;  
  97.                 jsonObject.put("url", map.get("url").toString());  
  98.                 jsonObject.put("dir", name);  
  99.                 jsonObject.put("title", map.get("title").toString());  
  100.                   
  101.                 // 翻译  
  102. //              String dateZh = super.translateEnToCinese(map.get("date")  
  103. //                      .toString());  
  104. //              String titleZh = super.translateEnToCinese(map.get("title")  
  105. //                      .toString());  
  106. //              json.put("title_zh_cn", dateZh + " - " + titleZh);  
  107.                   
  108.                 // 下载图片  
  109.                 super.downloadFile(map.get("url").toString(), dir + name);  
  110.                 tempJsonArray.put(jsonObject);  
  111.             }  
  112.             array.put(new JSONObject().put(path, tempJsonArray));  
  113.         } catch (Exception e) {  
  114.             e.printStackTrace();  
  115.         }  
  116.     }  
  117.   
  118.     public static void main(String[] args) throws Exception {  
  119.         new GoogleLogoCrawler().doStart();  
  120.     }  
  121.   
  122. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值