-
Notifications
You must be signed in to change notification settings - Fork 2
/
google_search_image.py
64 lines (50 loc) · 1.37 KB
/
google_search_image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2013 george
#
# Distributed under terms of the MIT license.
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "conf.settings")
import urllib
import urllib2
from cStringIO import StringIO
from PIL import Image
import json
#chick img_url is on live
def image_check(img_url):
try:
print img_url
img = StringIO(urllib2.urlopen(img_url).read())
Image.open(img)
return True
except:
return False
#get google search image
def search(keyword):
if not keyword:
return
api = "https://ajax.googleapis.com/ajax/services/search/images?"
data = dict()
data['v'] = '1.0'
data['q'] = keyword
data['imgsz'] = 'large'
data['rsz'] = 8
data['start'] = 0
data['imgtype'] = 'photo'
while True:
data['start'] += data['rsz']
url = api + urllib.urlencode(data)
result = json.loads(urllib2.urlopen(url).read())
if result.get('responseStatus') != 200:
print 'error'
break
img_infos = result.get('responseData', {}).get('results', [])
for img_info in img_infos:
if image_check(img_info.get('url')):
yield img_info
if __name__ == "__main__":
keys = ['黑鯊 fish']
for key in keys:
for img in search(key):
print img.get('url')