-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_scraper.py
358 lines (318 loc) · 9.93 KB
/
web_scraper.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import time
import sys
import os
import argparse
import concurrent.futures
#This will create a list of search terms:
# common_names = tree.xpath('//li/a[1][starts-with(@href, "/wiki/")]/text()')
# selects text from the first a tag nested within an li with an href values that starts with /wiki/
# Flora of California according to wikipedia
objects = ['Coast redwood',
'Giant sequoia',
'Bishop pine',
'Coulter pine',
'Gray pine',
'Knobcone pine',
'Ponderosa pine',
'Lodgepole pine',
'Monterey pine',
'Limber pine']
# 'Jeffrey pine',
# 'Parry pinyon',
# 'Shore pine',
# 'Sugar pine',
# 'Torrey pine',
# 'Western white pine',
# 'Single-leaf pinyon pine',
# 'Great Basin bristlecone pine',
# 'Foxtail pine',
# 'Monterey cypress',
# 'Santa Lucia fir',
# 'Douglas-fir',
# 'Bigcone Douglas-fir',
# 'California nutmeg',
# 'Incense cedar',
# 'Port Orford cedar-Lawson cypress',
# 'White fir',
# 'Mountain hemlock',
# 'Red fir',
# 'Pacific yew',
# 'Valley oak',
# 'Leather oak',
# 'Blue oak',
# 'California black oak',
# 'Canyon live oak',
# 'Interior live oak',
# 'Island oak',
# 'Engelmann oak',
# 'Coast live oak',
# 'California sycamore',
# 'White alder',
# 'Quaking aspen',
# 'Fremont cottonwood',
# 'Black cottonwood',
# 'Arroyo willow',
# 'Tanoak',
# 'California bay laurel',
# 'Madrone',
# 'Toyon',
# 'Bigleaf maple',
# 'Western blue elderberry',
# 'California Buckeye',
# 'Western redbud',
# 'California black walnut',
# 'California hazelnut',
# 'Chamise',
# 'Service-berry',
# 'Manzanita',
# 'California sagebrush',
# 'Coyote brush',
# 'Calliandra',
# 'California lilacs',
# 'Desert willow',
# 'Flannelbush',
# 'Creosote bush',
# 'Lupines',
# 'Snowberry',
# 'Huckleberry',
# 'Coffeeberry',
# 'Lemonade berry',
# 'Sugarbush',
# 'Gooseberries and currants',
# 'Sages',
# 'California fan palm',
# 'Joshua tree',
# 'California juniper',
# 'Blue palo verde',
# 'Yellow foothill palo verde',
# 'Single-leaf pinyon',
# 'Fremont cottonwood',
# 'Ocotillo',
# 'Creosote bush',
# 'Indian mallow',
# 'Brittlebush',
# 'Desert agave',
# 'California barrel cactus',
# 'Banana yucca',
# 'Mojave yucca',
# 'Rush milkweed',
# 'Purple desert sand-verbena',
# 'Sacred datura',
# 'California poppy',
# 'Douglas iris',
# 'Monkeyflower',
# 'Columbine',
# 'Coyote mint',
# 'Buckwheats',
# 'Western wild ginger',
# 'Pacific bleeding-heart',
# 'Island coral bells',
# 'Canyon coral bells',
# 'Heucherella',
# 'Threeleaf foamflower',
# 'Redwood sorrel',
# 'Polypody ferns',
# 'Native sword ferns',
# 'Giant chain fern',
# 'Goldback ferns',
# 'Wood ferns',
# 'Maidenhair ferns',
# "Ithuriel's spear",
# 'Meadow onion',
# 'Goldenstars',
# 'Brodiaeas',
# 'Blue dicks-ookow',
# 'Mariposa lilies',
# 'Baby blue eyes',
# 'Blazing star',
# 'California poppy',
# 'Chinese houses',
# 'Elegant clarkia',
# 'Farewell-to-spring',
# 'Meadowfoam',
# "Miner's lettuce",
# 'Tarweed',
# 'Wind poppy',
# "Dutchman's pipe vine",
# 'Morning glory',
# 'Chaparral clematis',
# "Western virgin's bower",
# 'Calabazilla',
# 'Wild cucumber-manroot',
# 'Cucamonga manroot-bigroot',
# 'California wild grape',
# 'Desert wild grape',
# 'Purple three-awn',
# 'Blue grama',
# 'California fescue',
# 'Idaho fescue',
# 'Red fescue',
# 'Junegrass',
# 'Giant wildrye',
# 'California melic',
# 'Deer grass',
# 'Purple needlegrass',
# 'Indian ricegrass',
# 'Pine bluegrass',
# 'Sedges',
# 'Rushes',
# 'Western blue-eyed grass',
# 'Chalk lettuce',
# 'Coast dudleya',
# 'Canyon live-forever',
# 'Fingertips',
# "Giant chalk dudleya",
# 'Lanceleaf liveforever',
# 'Broadleaf stonecrop',
# 'Coast sedum',
# 'Feather River stonecrop',
# 'Red Mountain stonecrop',
# 'Roseflower stonecrop',
# 'Sierra stonecrop']
# Taking command line arguments from users
parser = argparse.ArgumentParser()
# parser.add_argument('-k', '--keywords', help='delimited list input', type=str, required=True)
parser.add_argument('-l', '--limit', help='delimited list input', type=str, required=False)
parser.add_argument('-c', '--color', help='filter on color', type=str, required=False, choices=['red', 'orange', 'yellow', 'green',
'teal', 'blue', 'purple', 'pink', 'white', 'gray', 'black', 'brown'])
parser.add_argument('-s', '--size', help='filter on size', type=str, required=False, choices=['any', 'large', 'medium', 'icon'])
args = parser.parse_args()
# search_keyword = [str(item) for item in args.keywords.split(',')]
#setting limit on number of images to be downloaded
if args.limit:
limit = int(args.limit)
if int(args.limit) >= 100:
limit = 100
else:
limit = 100
# keyword_param = args.keywords.replace(' ', '+')
color_param = (',ic:specific,isc:' + args.color) if args.color else ''
size_param = (',isz:'+args.size[0]) if args.size else ''
# Downloading entire Web Document (Raw Page Content)
def download_page(url):
version = (3, 0)
cur_version = sys.version_info
if cur_version >= version: # If the Current Version of Python is 3.0 or above
import urllib.request # urllib library for Extracting web pages
try:
headers = {}
headers[
'User-Agent'] = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"
req = urllib.request.Request(url, headers=headers)
resp = urllib.request.urlopen(req)
respData = str(resp.read())
return respData
except Exception as e:
print(str(e))
else: # If the Current Version of Python is 2.x
import urllib2
try:
headers = {}
headers['User-Agent'] = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17"
req = urllib2.Request(url, headers=headers)
try:
response = urllib2.urlopen(req)
except URLError: # Handling SSL certificate failed
context = ssl._create_unverified_context()
response = urlopen(req,context=context)
page = response.read()
return page
except:
return "Page Not found"
# Finding 'Next Image' from the given raw page
def _images_get_next_item(s):
start_line = s.find('rg_di')
if start_line == -1: # If no links are found then give an error!
end_quote = 0
link = "no_links"
return link, end_quote
else:
start_line = s.find('"class="rg_meta"')
start_content = s.find('"ou"', start_line + 1)
end_content = s.find(',"ow"', start_content + 1)
content_raw = str(s[start_content + 6:end_content - 1])
return content_raw, end_content
# Getting all links with the help of '_images_get_next_image'
def _images_get_all_items(page):
items = []
while True:
item, end_content = _images_get_next_item(page)
if item == "no_links":
break
else:
# only want jpg images...
items.append(item) # Append all the links in the list named 'Links'
time.sleep(0.1) # Timer could be used to slow down the request for image downloads
page = page[end_content:]
return items
def save_images(obj):
keyword_param = obj.replace(' ', '+')
url = ('https://www.google.com/search?as_st=y&tbm=isch&hl=en&as_q="'+keyword_param+
'"&as_epq=&as_oq=&as_eq=&cr=&as_sitesearch=&safe=images&tbs=itp:photo'+size_param+color_param+',ift:jpg'
)
raw_html = (download_page(url))
time.sleep(0.1)
items = _images_get_all_items(raw_html)
print ("Total Image Links = " + str(len(items)))
dir_name = keyword_param.replace('+', '_')
os.makedirs(dir_name)
## To save imges to the same directory
# IN this saving process we are just skipping the URL if there is any error
k = 0
e = 0
while (k < limit+e): # I want limit num of images no matter what!!
try:
req = Request(items[k], headers={
"User-Agent": "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17"})
response = urlopen(req, None, 15)
image_name = str(items[k][(items[k].rfind('/'))+1:])
if '?' in image_name:
image_name = image_name[:image_name.find('?')]
if ".jpg" in image_name or ".png" in image_name or ".jpeg" in image_name or ".svg" in image_name:
output_file = open(dir_name + "/" + str(k + 1) + ". " + image_name, 'wb')
else:
output_file = open(dir_name + "/" + str(k + 1) + ". " + image_name + ".jpg", 'wb')
image_name = image_name + ".jpg"
data = response.read()
output_file.write(data)
response.close()
print("completed ====> " + str(k + 1) + ". " + image_name)
k += 1
except IOError: # If there is any IOError
errorCount += 1
e += 1
print("IOError on image " + str(k + 1))
k += 1
except HTTPError as e: # If there is any HTTPError
errorCount += 1
e += 1
print("HTTPError" + str(k))
k += 1
except URLError as e:
errorCount += 1
e += 1
print("URLError " + str(k))
k += 1
############## Main Program ############
t0 = time.time() # start the timer
version = (3,0)
cur_version = sys.version_info
if cur_version >= version: # If the Current Version of Python is 3.0 or above
# urllib library for Extracting web pages
from urllib.request import Request, urlopen
from urllib.request import URLError, HTTPError
else: # If the Current Version of Python is 2.x
# urllib library for Extracting web pages
from urllib2 import Request, urlopen
from urllib2 import URLError, HTTPError
# Download Image Links
errorCount = 0
with concurrent.futures.ProcessPoolExecutor() as executor:
# for obj in objects:
executor.map(save_images, objects)
print("\n")
print("Everything downloaded!")
t1 = time.time() # stop the timer
total_time = t1 - t0 # Calculating the total time required to crawl, find and download all the links of 60,000 images
print("Total time taken: " + str(total_time) + " Seconds")
print("Total Errors: "+ str(errorCount) + "\n")