java批量下載百度貼吧樓主上傳的圖片

來源:互聯網
上載者:User

還不夠完美,只能下載樓主上傳的圖片,樓主如果是盜鏈的則無法識別,還有的流的開閉太頻繁了.

import java.io.BufferedReader;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.URL;import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * 批量下載百度貼吧樓主上傳的圖片 * 如果圖片是樓主使用連結地址則無法下載 * 這時可以重寫正則匹配原則 * @author GeenkEmp01 *  */public class GetBaiDuTieBaPicture {private int index = 0;/** * @param args */public static void main(String[] args) {GetBaiDuTieBaPicture getP = new GetBaiDuTieBaPicture();String tieziUrl = "http://tieba.baidu.com/p/1212071711";String imageDirectory = "E:/baiduimage/";getP.getImageUrl(tieziUrl,imageDirectory);}/** *  * @param tieziUrl *            文章的網址 * @param imageDirectory *            儲存下載圖片的目錄 */public void getImageUrl(String tieziUrl, String imageDirectory) {int pn = getTotalPageNum(tieziUrl);URL url;Pattern p = Pattern.compile("http://imgsrc.baidu.com/forum/pic/item/[\\w,\\d]{40}.jpg");InputStream is = null;BufferedReader br = null;for (int i = 1; i <= pn; i++) {try {url = new URL(tieziUrl+"?pn=" + i);is = (InputStream) url.getContent();br = new BufferedReader(new InputStreamReader(is));String str = null;while ((str = br.readLine()) != null) {Matcher m = p.matcher(str);while (m.find()) {index++;String imageUrl = m.group();System.out.println(imageUrl);System.out.println("正在下載第" + index + "張圖片...");downloadImage(imageUrl, imageDirectory + index + ".jpg");}}} catch (Exception e) {e.printStackTrace();} finally {try {if (br != null)br.close();if (is != null)is.close();} catch (IOException e) {e.printStackTrace();}}}System.out.println("共下載了" + index + "張圖片");}/** * 擷取文章總共頁數 * @param tieziUrl * @return */public int getTotalPageNum(String tieziUrl) {int pageNum = 1;URL url;Pattern p = Pattern.compile("pn=\\d*\">尾頁<");InputStream is = null;BufferedReader br = null;try {url = new URL(tieziUrl);is = (InputStream) url.getContent();br = new BufferedReader(new InputStreamReader(is));String str = null;while ((str = br.readLine()) != null) {Matcher m = p.matcher(str);while (m.find()) {String s = m.group();pageNum = Integer.parseInt(s.substring(3, s.length()-5));}}} catch (Exception e) {e.printStackTrace();} finally {try {if (br != null)br.close();if (is != null)is.close();} catch (IOException e) {e.printStackTrace();}}System.out.println("文章共有"+pageNum+"頁");return pageNum;}/** * 下載一張圖片 * @param imageUrl * @param saveFile */public void downloadImage(String imageUrl, String saveFile) {URL url = null;OutputStream os = null;InputStream is = null;try {url = new URL(imageUrl);is = url.openStream();os = new FileOutputStream(saveFile);byte[] buff = new byte[1024];int readed;while ((readed = is.read(buff)) != -1) {os.write(buff, 0, readed);}} catch (Exception e) {e.getStackTrace();} finally {try {if (is != null)is.close();if (os != null)os.close();} catch (IOException e) {e.printStackTrace();}}}}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.