-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscrape.py
More file actions
382 lines (290 loc) · 12 KB
/
scrape.py
File metadata and controls
382 lines (290 loc) · 12 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# /usr/bin/env python
# coding: utf-8
'''
TODO: From/to page input options.
TODO: Database/file of successfully scraped urls to not scrape again.
'''
import bs4
import requests
from urlparse import urlparse, parse_qs
from time import sleep
from os.path import abspath, join as pathjoin, exists
from os import makedirs, stat
from itertools import chain
import re
import logging
import codecs
from urllib import urlencode
import sys
from optparse import OptionParser
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
DEFAULT_SLEEP_TIME = 1 # seconds
first = lambda it: iter(it).next()
def fileize(s):
return "".join((c for c in s if c.isalnum()))
def mkdirs(path):
try:
makedirs(path)
except (OSError, WindowsError):
pass
urlfile = pathjoin(abspath("."), "processed_urls.txt")
def load_processed_urls():
"""
Suppport resume by not re-downloading already downloaded product pages.
"""
if not exists(urlfile):
return set()
with codecs.open(urlfile, encoding='utf-8') as fp:
return set(fp.read().splitlines()) # remove the n from the end
PROCESSED = load_processed_urls()
def save_processed_urls(urls):
with codecs.open(urlfile, "w+", encoding="utf-8") as fp:
fp.write(u"\n".join(list(urls)))
def record_data(title, description, taxonomy, pid, fname=None):
"""
taxonomy: Hierarchical classification of product categories. From general
to more specific.
IE. [Category, Sub-Category, Sub-Sub-Category, ...]
"""
base = pathjoin(abspath("."), "data", fileize(first(taxonomy)))
mkdirs(base)
if fname is None:
fname = "{0}.txt".format(fileize(title))
fpath = pathjoin(base, fname)
logger.info(u"<record_data> Writing file: {0}".format(fpath))
if exists(fpath):
if stat(fpath).st_size == 0:
logger.warn(u"<record_data> File already exists, but has no data. "
u"Continuing.")
else:
logger.warn(u"<record_data> File already exists with data. "
u"Skipping.")
return
with codecs.open(fpath, "w+", encoding="utf-8") as fp:
lines = [
u"{0}\n".format(u", ".join(taxonomy)),
u"{0}\n".format(pid),
u"{0}\n".format(title),
u"{0}\n".format(description),
]
#fp.writelines([l.encode("utf-8") for l in lines])
fp.writelines(lines)
DIGIT_RE_C = re.compile(ur".*?(?P<pid>\d+).*")
def scrape_macys(murl, maxn=None):
logger.info("Scraping Product Listing Page {url} for products."
.format(url=murl))
parsed = urlparse(murl)
soup = bs4.BeautifulSoup(requests.get(murl).text) # .content
aelts = soup.select("div.shortDescription > a")
product_hrefs = [a.get('href') for a in aelts]
nscraped = 0
for href in product_hrefs:
if href is None:
logger.warning("None entry found in href list.")
continue
if href in PROCESSED:
logger.info("href already processed: {href}".format(href=href))
continue
logger.info("Scraping product page: {href}".format(href=href))
fullhref = "{0}://{1}{2}".format(parsed.scheme, parsed.netloc, href)
pdata = requests.get(fullhref)
soup = bs4.BeautifulSoup(pdata.text)
try:
product_title = first(soup.select("#productTitle")).text
product_ldesc = first(soup.select("#longDescription")).text
listitems = soup.select("ul#bullets > li")
product_ldesc = " ".join(chain((product_ldesc,),
(li.text for li in listitems)))
breadcrumbs = [a.text for a in soup.select(".breadCrumbs > a")]
pid_text = first(soup.select(".productID")).text
fname = fileize(product_title)
try:
pid = DIGIT_RE_C.match(pid_text).groupdict()['pid']
fname += "_" + pid
except AttributeError, attrerr:
logger.warning("No product ID found")
logger.error(str(attrerr))
fname += ".txt"
if all((product_title, product_ldesc, breadcrumbs)):
record_data(product_title, product_ldesc, breadcrumbs, pid,
fname=fname)
else:
logger.warning("Did not record data for product url: {href}"
.format(href=href))
except StopIteration:
pass
finally:
PROCESSED.add(href)
nscraped += 1
if maxn is not None and nscraped >= maxn:
break
logger.debug("Sleeping {0} seconds".format(DEFAULT_SLEEP_TIME))
sleep(DEFAULT_SLEEP_TIME)
return nscraped
def configure_logging(filename="scrape.log"):
root_logger = logging.getLogger()
lsh = logging.StreamHandler()
lfh = logging.FileHandler(filename)
fmt = logging.Formatter(
"%(asctime)s | %(levelname)s | %(module)s | %(message)s")
lsh.setFormatter(fmt)
lfh.setFormatter(fmt)
root_logger.addHandler(lsh)
root_logger.addHandler(lfh)
root_logger.setLevel(logging.DEBUG)
def fix_chars(string):
"""
Replace with punctuation that we will know to filter.
TODO: Support full unicode punctuation in NLP pipeline.
"""
# Replace lsquo (\x91)
#fixed = re.sub(u"\x91", u"‘", string)
fixed = re.sub(u"\x91", u"'", string)
# Replace rsquo (\x92)
#fixed = re.sub(u"\x92", u"’", fixed)
fixed = re.sub(u"\x92", u"'", fixed)
# Replace ldquo (\x93)
#fixed = re.sub(u"\x93", u"“", fixed)
fixed = re.sub(u"\x93", u'"', fixed)
# Replace rdquo (\x94)
#fixed = re.sub(u"\x94", u"”", fixed)
fixed = re.sub(u"\x94", u'"', fixed)
# Replace ndash (\x96)
#fixed = re.sub(u"\x96", u"–", fixed)
fixed = re.sub(u"\x96", u"-", fixed)
# Replace mdash (\x97)
#fixed = re.sub(u"\x97", u"—", fixed)
fixed = re.sub(u"\x97", u"-", fixed)
# Replace ellipsis char
fixed = re.sub(u"\x85", u"...", fixed)
return fixed
home_decor_url = \
"http://www1.macys.com/shop/for-the-home/home-decor?id=55971&edge=hybrid"
all_bath_url = \
"http://www1.macys.com/shop/bed-bath/shop-all-bath?id=8237&edge=hybrid"
all_fine_jewelery_url = \
"http://www1.macys.com/shop/jewelry-watches/fine-jewelry?id=21996&edge=hybrid"
all_diamond_jewelery_url = \
"http://www1.macys.com/shop/jewelry-watches/diamond-jewelry?id=67017&edge=hybrid"
sub_cat_url = ("http://www1.macys.com/catalog/index.ognc"
"?CategoryID={catid}&pageIndex={pg}")
'''
http://www.macys.com/catalog/index.ognc?CategoryID=22672&cm_sp=us_hdr-_-homepage-_-22672_for-the-home
Each sub-category
http://www1.macys.com/shop/for-the-home/home-decor?id=55971&edge=hybrid
For each sub-sub-category
http://www1.macys.com/shop/for-the-home/bowls-vases?id=55973&edge=hybrid
Same thing, with pagination
http://www1.macys.com/catalog/index.ognc?CategoryID=55973&pageIndex=2
'''
P_CATS = ("home-decor", "bath", "fine-jewelery", "diamonds-jewelery")
P_URLS = (home_decor_url, all_bath_url, all_fine_jewelery_url,
all_diamond_jewelery_url)
P_CAT_TO_URL = dict(zip(P_CATS, P_URLS))
def main():
configure_logging()
usage = ("%prog [list of catagories]")
epilog = ('\nAvailable categories include: "{cats}". '
"Specify \"all\" to scrape all categories.\n"
"Examples:\n"
"{prog} home-decor bath\n"
"{prog} all\n"
).format(cats='", "'.join(P_CATS), prog=sys.argv[0])
description="Macy's Product Scraper CLI."
class MyParser(OptionParser):
def format_epilog(self, formatter):
return self.epilog
parser = MyParser(usage=usage, description=description, epilog=epilog)
(option, args) = parser.parse_args()
# TODO: Replace with optparse
if args[0] == "all":
cats = P_CATS
else:
cats = set(P_CATS)
for cat in args:
if cat not in cats:
parser.error("Invalid category specification.")
cats = args
for cat in cats:
process_category(P_CAT_TO_URL[cat])
def process_category(url, max_scrape=20000):
try:
soup = bs4.BeautifulSoup(requests.get(url).text)
main_parsed = urlparse(url)
nscraped = 0
# urls for sub-sub-category pages
cat_urls = [a.attrs['href'] for a in soup.select("a.facet-item")]
# each category url has an 'id' query parameter to use in the
# CategoryID query parameter of the next url
for cat_url in cat_urls:
cat_parsed = urlparse(cat_url)
if not cat_parsed.netloc:
cat_href = "{0}://{1}{2}".format(main_parsed.scheme,
main_parsed.netloc, cat_url)
else:
cat_href = cat_url
cat_parsed = urlparse(cat_href)
cat_qs = parse_qs(cat_parsed.query)
#cat_href_1 = sub_cat_url.format(catid=first(cat_qs['id']), pg=1)
cat_href_1 = cat_href
soup = bs4.BeautifulSoup(requests.get(cat_href_1).text)
nscraped_cat = 0
# We already loaded the first page of items
nscraped_cat += scrape_macys(cat_href_1)
nscraped += nscraped_cat
logger.info("Scraped {n} products so far".format(n=nscraped))
# Discovery Category ID
# <a href="http://www1.macys.com/catalog/index.ognc?CategoryID=55971
# &pageIndex=2">2</a>
try:
ahref = [a.attrs['href'] for a in soup.select("div.pagination > a")
if a.text.isnumeric()][0]
except IndexError:
# No pages
continue
m = re.match(ur".*?CategoryID=(\d+).*", ahref)
catid = m.group(1)
parentid = soup.select("div.parentCategories")[0].attrs['id']
logger.info("Discovering the number of pages at url: {url}"
.format(url=cat_href_1))
npages = max([int(pageno) for pageno in
[a.text for a in soup.select("div.pagination > a")]
if pageno.isnumeric()] or [0])
logger.info("Number of pages found: {n}".format(n=npages))
for page in range(2, npages+1): # do the rest
if nscraped_cat >= max_scrape:
break
plisturl = (
"http://www1.macys.com/catalog/category/facetedmeta"
"?edge=hybrid&"
"parentCategoryId={parentid}&"
"categoryId={catid}&"
"multifacet=true&"
"pageIndex={pg}&"
"sortBy=ORIGINAL&"
"productsPerPage=40&")
product_ids = requests.get(plisturl
.format(parentid=parentid, catid=catid, pg=page))\
.json()['productIds']
pageurl = (
"http://www1.macys.com/shop/catalog/product/thumbnail/1?"
"edge=hybrid&limit=none&suppressColorSwatches=false&"
"categoryId={catid}&"
"ids={ids}")
# `ids` is a comma-separated list of <parentid>_<itemid> strings.
# The top level product page uses this ajax request to continue
# to request details of more products (pagination).
ids = ",".join(["_".join((str(catid), str(pid)))
for pid in product_ids])
nscraped_cat += scrape_macys(
pageurl.format(catid=catid, ids=ids))
nscraped += nscraped_cat
logger.info("Scraped {n} products so far".format(n=nscraped))
logger.info("Scraping Complete")
finally:
save_processed_urls(PROCESSED)
if __name__ == "__main__":
main()
#logger.error("Valid categories: " + ", ".join(P_CATS + ("all",)))
#sys.exit(1)