-
Notifications
You must be signed in to change notification settings - Fork 1
/
amazon.py
159 lines (128 loc) · 5.2 KB
/
amazon.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
# -*- coding: utf-8 -*-
import urllib
from pyquery import PyQuery as pq
import pdb
import re
import json
import sys
import argparse
import traceback
import os
import settings
# Create a folder and optionally delete the old one
def createFolder(path, overwrite=True):
if os.path.exists(path):
if (overwrite):
import shutil
shutil.rmtree(path)
os.makedirs(path)
else:
os.makedirs(path)
# Download an image to a selected path
def downloadImage(folder_path, image_name, url, overwrite=True):
createFolder(folder_path, overwrite)
path = folder_path + '/' + image_name
f = open(path, 'wb')
f.write(urllib.urlopen(url).read())
f.close()
# Go to the detail view of the project and harvest more information
def getProductDetail(aid, link, path):
query = pq(url=link)
details = {}
details['images'] = {}
try:
regex = re.compile(ur'(data\["colorImages"\] = )(.*)(;)', re.MULTILINE)
images = re.findall(regex, query.html().encode('utf-8').strip())
details['images'] = json.loads(images[0][1])
if (path):
for image in details['images']:
for variant in details['images'][image]:
downloadImage(path + '/' + image, variant['hiRes'].rsplit('/', 1)[1], variant['hiRes'], False)
except:
print 'No images found'
regex = re.compile(r'[\n\r\t]')
description = query('div').filter('#productDescription.a-section').html()
if (description):
description = description.encode('utf-8').strip()
description = re.sub("(<!--.*?-->)", "", description, flags=re.MULTILINE)
elif (not description):
description = ''
details['description'] = regex.sub('', description).strip()
myKeywords = ''
try:
regex = re.compile(ur'(<meta name="keywords" content=")(.*)("\/>)', re.MULTILINE)
keywords = re.findall(regex, query.html().encode('utf-8').strip())
myKeywords = keywords[0][1].split(',')
except:
print 'No keywords found'
details['keywords'] = myKeywords
myRating = ''
try:
rating = query('span').filter('#acrPopover').attr('title').strip().split(' ')[0]
myRating = rating[0]
except:
print 'No rating found'
details['rating'] = myRating
return details
# Go to the list view and iterate through the product list
def getProductInfo(keyword, page, download):
products = []
try:
link = settings.AMAZON_URL.format(keyword, str(page))
query = pq(url=link)
results = query('li').filter('.s-result-item')
path = None
for result in results:
try:
article = {}
articleQuery = pq(result)
article['amazonID'] = articleQuery.attr('data-asin')
myTitle = ''
if (articleQuery('h2').filter('.s-access-title').contents()[0]):
myTitle = articleQuery('h2').filter('.s-access-title').contents()[0].strip()
article['title'] = myTitle
article['thumbnail'] = articleQuery('img').filter('.s-access-image').attr["src"]
if (download):
path = download + '/' + article['amazonID']
downloadImage(path, article['thumbnail'].rsplit('/', 1)[1], article['thumbnail'])
article['url'] = articleQuery('a').filter('.s-access-detail-page').attr["href"]
article['details'] = getProductDetail(article['amazonID'], article['url'], path)
price = articleQuery('span').filter('.s-price').contents()[0].split(' ')
article['price'] = {'currency': price[0], 'value': price[1]}
products.append(article)
except Exception, err:
import traceback
traceback.print_exc()
print 'Invalid Product'
print Exception, err
except Exception, err:
print Exception, err
return products
def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument('-o', action='store', dest='output', help='Output File', default=settings.DEFAULT_OUTPUT_FOLDER + settings.DEFAULT_OUTPUT_FILENAME)
parser.add_argument('-k', action='store', dest='keyword', help='Keyword Search')
parser.add_argument('-p', action='store', dest='pages', help='Num Pages', type=int)
parser.add_argument('-d', action='store', dest='download', help='Download Images Folder')
args = parser.parse_args()
results = []
currentPage = 1
download = None
if (args.download):
download = args.download
if (not download.startswith('/')):
download = settings.DEFAULT_OUTPUT_FOLDER + download
createFolder(download)
if (args.keyword):
if (args.pages):
for num in range(1, args.pages + 1):
results = results + getProductInfo(args.keyword, num, download)
else:
results = getProductInfo(args.keyword, 1, download)
if (args.output):
import codecs
with codecs.open(args.output, 'w', encoding='utf8') as f:
f.write(json.dumps(results))
f.close()
if __name__ == "__main__":
main(sys.argv)