Today's Question:  What does your personal desk look like?        GIVE A SHOUT

Java code to retrieve Bing background image path

  Pi Ke        2012-05-02 10:51:51       8,100        0    

When Microsoft presented their search engine Bing, this new design gave us some surprise, especially its background images, they are very beautiful and it will change every day. But   unfortunately we cannot save the image onto our PC by right clicking the mouse.  After some research on its source code, I found a feasible but not so sophisticated way to achieve this, we can retrieve the image path from the source code and then use this path we can download the image. This is just to show you how we can save the background image, please do not use it on commercial products since these images are copyrighted products of Microsoft.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class BingBGImagePathRetriever {
    public static void main(String[] args) {
        String imgSrc="";
        try {
            URL htmlUrl = new URL("http://www.bing.com");
            URLConnection htmlConn = htmlUrl.openConnection();
            htmlConn.connect();
            HttpURLConnection htmlHttpConn = (HttpURLConnection) htmlConn;

            if ((int) htmlHttpConn.getResponseCode() != 200) {
                System.out.println("Cannot open connection.");
            } else {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(htmlConn.getInputStream()));
                String str = "";
                while ((str = reader.readLine()) != null) {
                    if (str.lastIndexOf("{url:") > 0) {
                        imgSrc = str.substring((str.lastIndexOf("{url:") + 7),
                                (str.lastIndexOf(".jpg',") + 4));
                    }
                }
                imgSrc = "http://www.bing.com" + imgSrc; // The source image url from Bing search engine.
                System.out.println(imgSrc);
            }
        } catch (Exception ioEx) {
            ioEx.printStackTrace();
        }
    }
}

The code is quite simple, basically what it does is first it retrieve the source code of the home page of Bing and then it parse the source code and find the position of background image path, then use some string functions to build the complete path string.


JAVA  DOWNLOAD  URL  BING  BACKGROUND IMAGE PATH  SAVE 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  0 COMMENT


No comment for this article.