-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgSpider.py
66 lines (39 loc) · 1.1 KB
/
imgSpider.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
import urllib
import urllib2
def get_after(string,first,symbol="""):
try:
string+=" "
start = string.index(first) + len(first)
second = string.index(symbol,start)
if second - start <= 1:
second+= (1 if string[second + len(symbol)] == " " else 0)
return string[second + len(symbol):string.index(" ",second+len(symbol))]
else:
return ""
except ValueError:
return ""
def image_type(pop):
if ".GIF" in pop.upper():
return ".gif"
elif ".JPG" in pop.upper():
return ".jpg"
elif ".JPEG" in pop.upper():
return ".jpg"
elif ".PNG" in pop.upper():
return ".png"
elif ".WEBM" in pop.upper():
return ".webm"
WHAT=raw_input("Enter term: ")
try:
MANY =int(raw_input("How many?: "))
except:
print ("Not a vaild number defaulting to 1")
MANY = 1
response = urllib2.urlopen('http://www.bing.com/images/search?q='+WHAT)
html = response.read()
chunk = get_after(html,"imgurl:")
for i in range(0,MANY):
url = chunk[:chunk.upper().find(""")]
print url
urllib.urlretrieve(url, WHAT+str(i)+image_type(url))
chunk = get_after(html[html.index(chunk)+len(chunk):],"imgurl:")