searching google images from python
Question: 
    #!/usr/bin/env python
    
    
    import urllib
    import mechanize
    from bs4 import BeautifulSoup
    from urlparse import urlparse
    
    def getPic(search):
        search = search.replace(" ","%20")
        try:
                browser = mechanize.Browser()
                browser.set_handle_robots(False)
                browser.addheaders = [('User-Agent','Mozilla')]
    
                htmltext = browser.open("https://www.google.com/search?site=&tbm=isch&source=hp&biw=1855&bih=990&q=" + search + "&oq=" +search)
                img_url = []
                formatted_images = []
                soup = BeautifulSoup(htmltext)
                results = soup.findAll("a")
                for r in results:
                        try:
                                if "imgres?imgurl" in r['href']:
                                        img_url.append(r['href'])
                        except:
                                a=0
                for im in img_url:
                        refer_url = urlparse(str(img_url[0]))
                        return refer_url.query.split("&")[0].replace("imgurl=","")
                return  formatted_images
    
    
        except:
                print "error"
    print getPic("occupy wall street")
    

Instead of getting the link of an image as output I'm getting "[]" as an
output.Can someone figure out what's the problem with my code.


Answer: Google sends `"imgres?imgurl"` only to browser with JavaScript  
but `mechanize.Browser()` is like browser without JavaScript.

Turn off JavaScript in your browser and see HTML send by Google.

