Java and Python download Bing homepage picture One, first of all Java code
Importorg.apache.http.HttpEntity;ImportOrg.apache.http.client.methods.CloseableHttpResponse;ImportOrg.apache.http.client.methods.HttpGet;ImportOrg.apache.http.impl.client.CloseableHttpClient;Importorg.apache.http.impl.client.HttpClients;ImportOrg.apache.http.util.EntityUtils;Importjava.io.*;ImportJava.text.SimpleDateFormat;ImportJava.util.Date;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern; Public class downloadimg { Public Static void Writeimgentitytofile(Httpentity imgentity,string fileaddress) {File StoreFile =NewFile (fileaddress); FileOutputStream output =NULL;Try{output =NewFileOutputStream (StoreFile);if(Imgentity! =NULL) {InputStream instream; Instream = Imgentity.getcontent ();byteB[] =New byte[8*1024x768];intCount while((count = Instream.read (b))! =-1) {Output.write (b,0, count); } } }Catch(FileNotFoundException e) {E.printstacktrace (); }Catch(IOException e) {E.printstacktrace (); }finally{Try{Output.close (); }Catch(IOException e) {E.printstacktrace (); } } } Public Static void Main(string[] args) {System.out.println ("Get Bing pictures in the address ..."); SimpleDateFormat DateFormat =NewSimpleDateFormat ("Yyyy-mm-dd"); Closeablehttpclient httpClient = Httpclients.createdefault (); HttpGet HttpGet =NewHttpGet ("http://cn.bing.com/"); Closeablehttpresponse response =NULL;Try{response = Httpclient.execute (HttpGet); Pattern p = pattern.compile ("Http://.*?\\.jpg"); Matcher m = P.matcher (entityutils.tostring (Response.getentity ())); String address =NULL;if(M.find ()) {address = M.group (); }Else{System.exit (0); } System.out.println ("Picture address:"+ address); System.out.println ("Downloading ..."); HttpGet GetImage =NewHttpGet (address); Closeablehttpresponse responseimg = Httpclient.execute (getImage); httpentity entity = responseimg.getentity (); Writeimgentitytofile (Entity,dateformat.format (NewDate ()) +". jpg"); System.out.println ("Download complete."); }Catch(IOException e) {E.printstacktrace (); }finally{Try{Httpclient.close (); Response.close (); }Catch(IOException e) {E.printstacktrace (); } } }}
Two, Python code
#!/usr/bin/python#encoding: Utf-8ImportTimeImportUrllibImportrehtml = Urllib.urlopen ("http://cn.bing.com/"). Read () imgaddress = Re.search (r "Http://.*?\.jpg", HTML)ifImgaddress:filename = Time.strftime ("%y-%m-%d") +". jpg" Print "The address of Bing Pictures today is:"+ Imgaddress.group ()Print "Downloading ..."Urllib.urlretrieve (Imgaddress.group (), FileName)Print "Download complete!"+"Save As"+ FileNameElse:Print "There seems to be a problem today ..."
Java and Python download Bing home page pictures