Java and Python download Bing home page pictures

Source: Internet
Author: User
Tags dateformat

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

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.