Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # adv_google_image_search.py -- ZZZ Tk thumbnail view with external image size referencing to be added
- import json, urllib2, os, sys, re
- def getImgSearchUrl(searchTerm, startPage=0):
- searchTerm = searchTerm.replace(' ','%20') #simple convertion
- results = ''
- return 'https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q={0}&start={1}&imgsz=huge'\
- .format(searchTerm, startPage)
- def getUrlContent(url):
- try:
- f = urllib2.urlopen(url)
- s = f.read()
- f.close()
- except Exception as e:
- print >> sys.stderr, e, url
- return None
- return s
- def downloadRessource(url, destPath='', fileName=None):
- if fileName == None:
- fileName = os.path.basename(url) #default filename
- content = getUrlContent(url)
- if content:
- try:
- with open(destPath+fileName, 'wb') as f:
- f.write(content)
- f.close()
- except: print "\n??? Unable To Download Image"
- def getPics(subject, destPath='', nbPages=12):
- for pageNb in xrange(nbPages):
- url = getImgSearchUrl(subject, pageNb*8)
- response = getUrlContent(url)
- if response:
- try: # Skip if 404
- jsonInfo = json.loads(response)
- results = jsonInfo['responseData']['results']
- for res in results:
- #print res['titleNoFormatting']
- #print res['contentNoFormatting']
- print '\n>>>>>', res['width'], res['height']
- urlres = re.findall('''\S+\.jpe?g''',res['url'])[0]
- print urlres
- #downloadRessource(res['url'], destPath)
- except: print "\n??? Unable To Retrieve URL Data"
- getPics('Sexy Adult Wonder Woman costume -kids -children -men -boys -shirt')
- getPics('top 10 3D car wallpapers')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement