-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape_images.py
executable file
·270 lines (227 loc) · 8.7 KB
/
scrape_images.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# NAME: scrape_images.py
# ONELINER: Script to scrape images from Reef2Reef.com
# LANGUAGE: Python 3.7
# AUTHOR: Megan McGee
# CREATED: 2020-06-30
#
# DESCRIPTION:
# This script was designed to get all of the images and names of corals
# posted for sale during Reef2Reef live sales. These images and names can
# then be used in future machine learning and deep learning projects.
#
# This script scrapes images. To use this, supply a url from Reef2Reef.com
# from which to scrape, the Reef2Reef author name for the posts to scrape,
# and the image location from the data-url. This script will go through all
# of the pages of the forum thread, download the images posted by the
# specified poster, and append the names to a text file.
#
# Note that this script may download many image files. Use at your own risk.
#
import argparse
import csv
import os
import re
import time
from bs4 import BeautifulSoup as bs
import requests
from urllib.parse import urlparse
from urllib.request import urlretrieve
def is_url_valid(url):
"""
Check if the format of the given url is valid.
Checks for scheme and netloc, then checks for status code.
Parameters:
url (string): A string containing the url
Returns:
is_valid (boolean): True if valid, else False
"""
parsed = urlparse(url)
is_valid = False
if bool(parsed.netloc) and bool(parsed.scheme):
if requests.get(url).status_code == 200:
return True
return is_valid
def get_all_images(url, poster, image_loc, do_verbose):
"""
Returns all image urls from the given url.
Parameters:
url (string): A string containing the url
poster (string): A string containing the Reef2Reef author name of
the post, such as 'WWC-BOT'
image_loc (string): A string containing the location of the images,
such as 'worldwidecorals.sirv.com/TSLS_20' from the data-url
for the image
do_verbose (boolean): A boolean, where True displays additional
debugging output
Returns:
image_links (list): A list of [names, image urls] from the page
"""
soup = bs(requests.get(url).content, "html.parser")
soup_div = soup.find_all('div',
attrs={'class':'message-userContent lbContainer js-lbContainer',
'data-lb-caption-desc':re.compile(r'^%s'%poster)})
# extract links to each of the images
# when find an image, also extract the name for labels in training
images=[]
names=[]
for tt in soup_div:
img_found = 0
try:
t1 = tt.find_all('img')
for image in t1:
if image_loc in image['data-url']:
images.append('https://www.reef2reef.com' + image['src'])
img_found = 1
if img_found == 1:
t2 = tt.find_all('b')
for name in t2:
names.append(name.text)
except:
pass
names_trimmed = names[0::6]
if do_verbose:
print('images: ', '\n', images)
print('names: ', '\n', names)
print('names_trimmed: ', '\n', names_trimmed)
# if the lengths of the two lists are the same, zip together
if len(names_trimmed) == len(images):
image_links = [list(i) for i in zip(names_trimmed, images)]
else:
image_links = []
print('WARNING: images and names are not the same length',
'\n', 'no image_links this page')
return image_links
def download_images(image_links, do_verbose):
"""
Downloads all images provided in the list of image names and urls.
Parameters:
image_links (list): A list of [names, image urls]
do_verbose (boolean): A boolean, where True displays additional
debugging output
"""
for i in image_links:
r = requests.get(i[1])
filename = os.path.join('./scraped_images/',
urlparse(i[1]).query.split('%2F')[-1].split('.jpg')[0]+'.jpg')
if do_verbose:
print('image_link: ', i)
print('filename: ', filename)
with open(filename, 'wb') as outfile:
outfile.write(r.content)
def output_names_files(image_links):
"""
Outputs a csv file with the coral name and filename.
Parameters:
image_links (list): A list of [names, image urls]
Returns:
coral_names_files.csv (file):
"""
with open('coral_names_files.csv', 'a', newline='') as f:
writer = csv.writer(f, delimiter='|')
writer.writerows(image_links)
def get_num_pages(url):
"""
Returns the number of pages in this thread of the forum.
Parameters:
url (string): A string containing the url
Returns:
num_pages (int): The number of pages
"""
soup = bs(requests.get(url).content, "html.parser")
nums=[]
soup_ul = soup.find_all('ul', attrs={'class':'pageNav-main'})
for tt in soup_ul:
t1 = tt.find_all('a')
for num in t1:
try:
nums.append(int(num.text))
except:
pass
num_pages = max(nums)
return num_pages
def main(url, poster, image_loc, do_verbose):
"""
Loops through the pages in this thread of the forum to get all images.
Parameters:
url (string): A string containing the url with the starting page
for scraping
poster (string): A string containing the Reef2Reef author name
of the post, such as 'WWC-BOT'
image_loc (string): A string containing the location of the
images, such as 'worldwidecorals.sirv.com/TSLS_20' from the
data-url for the image
do_verbose (boolean): A boolean, where True displays additional
debugging output
"""
# find first page, last page, and base url
if is_url_valid(url):
last_page = get_num_pages(url)
if do_verbose:
print('last page: ', last_page)
base_url, first_page = url.split('/page-')
first_page = int(first_page)
if do_verbose:
print('first page: ', first_page)
# loop through pages from first to last, pausing periodically
print('Beginning scrape at page ', first_page)
for i in range(first_page, last_page+1):
if do_verbose:
print('i: ', i)
# implement periodic pause
if i % 20 == 0:
print('pausing for 30 seconds at page ', i)
time.sleep(30)
# get url for the current page of the forum and scrape
current_url = base_url + '/page-' + str(i)
if is_url_valid(current_url):
image_links = get_all_images(current_url, poster, image_loc, do_verbose)
if image_links == []:
print('page ', i, ' has no image links')
download_images(image_links, do_verbose)
output_names_files(image_links)
else:
print('WARNING: page ', i, ' has invalid url')
print('End of scrape.')
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
'-u', '--url',
dest='url', action='store', type=str,
help='Specify url from which to scrape.',
required=True)
parser.add_argument(
'-p', '--poster',
dest='poster', action='store', type=str,
help='Specify author name of the post.',
required=True)
parser.add_argument(
'-i', '--image_loc',
dest='image_loc', action='store', type=str,
help='Specify the location of the images from the data-url.',
required=True)
parser.add_argument(
'-q', '--quit',
dest='do_quit', action='store_true',
help='Display informational messages then quit.',
default=False)
parser.add_argument(
'-v', '--verbose',
dest='do_verbose', action='store_true',
help='Display additional debugging output.',
default=False)
args = parser.parse_args()
do_quit = args.do_quit
do_verbose = args.do_verbose
if do_quit or do_verbose:
print('Input Arguments:')
print(' do_quit : ', do_quit)
print(' url : ', args.url)
print(' poster : ', args.poster)
print(' image_loc : ', args.image_loc)
print(' do_verbose : ', args.do_verbose)
if do_quit:
raise SystemExit('\n --- Requested quit --- \n')
main(args.url, args.poster, args.image_loc, do_verbose)