Java batch download pictures uploaded by Baidu Post Bar

Source: Internet
Author: User

Not perfect yet. You can only download images uploaded by the main user. If the main user is leeching, it cannot be identified, and other streams are too frequently opened or closed.

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; /*** batch download the images uploaded by the Baidu post bar poster * If the images are from the author's link address, they cannot be downloaded * at this time, you can rewrite the regular expression matching principle * @ author geenkemp01 **/Public CIA SS 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 * Post URL * @ Param imagedirectory * stores the downloaded image directory */Public void getimageurl (stri Ng tieziurl, string imagedirectory) {int Pn = gettotalpagenum (tieziurl); Url URL; pattern P = pattern. compile ("plugin"); 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 ("downloading the" + index + "image... "); 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 ("downloaded" + index + "Images ");} /*** get the total number of pages of a post * @ Param tieziurl * @ return */Public int gettotalpagenum (string tieziurl) {int pagenum = 1; Url URL; pattern P = pattern. compile ("Pn = \ D * \"> last page <"); inputstream is = NULL; bufferedreader BR = NULL; try {url = new URL (tieziurl ); is = (inputstream) URL. getcontent (); BR = new bufferedrea Der (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 ("Total posts" + pagenum + "page"); Return pagenum ;} /*** download an image * @ 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 ();}}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.