-
Notifications
You must be signed in to change notification settings - Fork 1
/
scraper.py
205 lines (194 loc) · 10.6 KB
/
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
import sys
reload(sys)
sys.setdefaultencoding('UTF8')
import requests
from bs4 import BeautifulSoup as bs
import scraperwiki
from datetime import datetime
import re
import time
start_url = 'http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords={}'
ua = {'User-agent': 'Mozilla/5.0'}
def connect(start_url, search_term):
search_page = requests.get(start_url.format(search_term), headers=ua)
soup = bs(search_page.text, 'lxml')
title = soup.title.text
while 'Robot Check' in title:
search_page = requests.get(start_url.format(search_term), headers=ua)
soup = bs(search_page.text, 'lxml')
title = soup.title.text
if soup:
pass
else:
connect(start_url, search_term)
return soup
def parse(search_term, search_tag, p):
soup = connect(start_url, search_term)
print p
search_tag = '='+'"'+search_tag+'"'
search_rows = soup.find_all('li', 's-result-item celwidget')
for search_row in search_rows:
search_term = search_term
search_tag = search_tag
try:
title = search_row.find('h2', 'a-size-medium a-color-null s-inline s-access-title a-text-normal').text.strip()
asin = search_row.find('a', 'a-link-normal s-access-detail-page a-text-normal')['href'].split('dp/')[-1].split('/')[0]
#asin = '='+'"'+search_row.find('a', 'a-link-normal s-access-detail-page a-text-normal')['href'].split('dp/')[-1].split('/')[0]+'"'
except: continue
#print(title)
pubdate = ''
try:
pubdate = search_row.find('div', 'a-row a-spacing-small').find('span', 'a-size-small a-color-secondary').text.strip().replace('by', '')
except:
pass
author = ''
try:
author = search_row.find('div', 'a-row a-spacing-small').find('div', 'a-row a-spacing-none').text.strip().split('by')[-1].strip()
except:
pass
item_format = ''
try:
item_format = search_row.find('h3', 'a-size-small a-color-null s-inline a-text-normal').text.strip()
except:
pass
price_rent = ''
try:
price_rent = search_row.find('div', 'a-fixed-left-grid-col a-col-right').find(text=re.compile('to rent')).find_previous('span', 'a-size-base a-color-price s-price a-text-bold').text
except:
pass
price = ''
try:
price = search_row.find('div', 'a-fixed-left-grid-col a-col-right').find(text=re.compile('to buy')).find_previous('span', 'a-size-base a-color-price s-price a-text-bold').text
except:
try:
price = search_row.find('div', 'a-fixed-left-grid-col a-col-right').find('span', 'a-size-small a-color-secondary a-text-strike').find_previous('span', 'a-size-base a-color-price s-price a-text-bold').text
except:
pass
low_price = ''
try:
low_price = search_row.find('div', 'a-fixed-left-grid-col a-col-right').find(text=re.compile('used & new')).find_previous('span', 'a-size-base a-color-price a-text-bold').text
except:
pass
offer_count = ''
try:
offer_count = search_row.find('div', 'a-fixed-left-grid-col a-col-right').find(text=re.compile('offers')).replace('(', '').replace(')','')
except:
pass
other1format = ''
try:
other1format = search_row.find('div', 'a-fixed-left-grid-col a-col-right').find('div', 'a-column a-span7').find('hr', 'a-divider-normal s-result-divider').find_next('a').text
except:
pass
if other1format:
other1asin = search_row.find('div', 'a-fixed-left-grid-col a-col-right').find('div', 'a-column a-span7').find('hr', 'a-divider-normal s-result-divider').find_next('a')['href'].split('dp/')[-1].split('/')[0]
#other1asin = '='+'"'+search_row.find('div', 'a-fixed-left-grid-col a-col-right').find('div', 'a-column a-span7').find('hr', 'a-divider-normal s-result-divider').find_next('a')['href'].split('dp/')[-1].split('/')[0]+'"'
else:
other1asin = ''
other2format = ''
try:
other2format = search_row.find('div', 'a-fixed-left-grid-col a-col-right').find('div', 'a-column a-span7').find(text=re.compile('Other Format')).find_next('a').text
except:
pass
if other2format:
other2asin = search_row.find('div', 'a-fixed-left-grid-col a-col-right').find('div', 'a-column a-span7').find(text=re.compile('Other Format')).find_next('a')['href'].split('dp/')[-1].split('/')[0]
#other2asin = '='+'"'+search_row.find('div', 'a-fixed-left-grid-col a-col-right').find('div', 'a-column a-span7').find(text=re.compile('Other Format')).find_next('a')['href'].split('dp/')[-1].split('/')[0]+'"'
else:
other2asin = ''
if other1format == other2format:
other1format = ''
other1asin = ''
comma = ''
try:
comma = search_row.find('div', 'a-fixed-left-grid-col a-col-right').find('div', 'a-column a-span7').find(text=re.compile('Other Format')).find_next('a').find_next('span', 'a-size-small a-color-secondary').text
except:
pass
other3format = ''
if ',' in comma:
other3format = ''
try:
other3format = search_row.find('div', 'a-fixed-left-grid-col a-col-right').find('div', 'a-column a-span7').find(text=re.compile('Other Format')).find_next('a').find_next('a').text
except:
pass
if other3format:
other3asin = search_row.find('div', 'a-fixed-left-grid-col a-col-right').find('div', 'a-column a-span7').find(text=re.compile('Other Format')).find_next('a').find_next('a')['href'].split('dp/')[-1].split('/')[0]
#other3asin = '='+'"'+search_row.find('div', 'a-fixed-left-grid-col a-col-right').find('div', 'a-column a-span7').find(text=re.compile('Other Format')).find_next('a').find_next('a')['href'].split('dp/')[-1].split('/')[0]+'"'
else:
other3asin = ''
newer = ''
try:
newer = search_row.find('div', 'a-fixed-left-grid-col a-col-right').find('div', 'a-column a-span7').find('a', text=re.compile('See newer edition of this book'))
except:
pass
if newer:
newer = '='+'"'+newer['href'].split('dp/')[-1].split('/')[0]+'"'
tradein = ''
try:
tradein = search_row.find('div', 'a-fixed-left-grid-col a-col-right').find(text=re.compile('Trade in yours for')).find_next('span', 'a-color-price').text
except:
pass
new_stock_count = new_stock_status = new_restock_date = ''
try:
new_stock_count = search_row.find('span', text=re.compile('left in stock - order soon')).text.strip()
except:
try:
new_stock_count = search_row.find(text=re.compile('Get it by')).parent.text
except:
try:
new_stock_count = search_row.find('span', text=re.compile('Temporarily out of stock')).text.strip()
except:
try:
new_stock_count = search_row.find('span', text=re.compile('Currently unavailable')).text.strip()
except:
try:
new_stock_count = search_row.find('span', text=re.compile('may require an extra')).text.strip()
except:
try:
new_stock_count = search_row.find('span', text=re.compile('Available for Pre-order')).text.strip()
except:
try:
new_stock_count = search_row.find('span', text=re.compile('In stock on')).text.strip()
except:
pass
if 'left in stock - order soon' in new_stock_count:
new_stock_count = new_stock_count.split('left')[0].split('Only')[-1].strip()
new_stock_status = 'limited'
new_restock_date = 'na'
elif 'Get it by' in new_stock_count:
new_stock_count = 999
new_stock_status = 'in stock'
new_restock_date = None
elif 'Temporarily out of stock' in new_stock_count:
new_stock_count = 0
new_stock_status = 'Temporarily out'
new_restock_date = 'temp'
elif 'Currently unavailable' in new_stock_count:
new_stock_count = 0
new_stock_status = 'unavailable'
new_restock_date = 'unknown'
elif 'may require an extra' in new_stock_count:
new_restock_date = new_stock_count.split('an')[-1].strip().split('to')[0].strip()
new_stock_count = 0
new_stock_status = 'extra processing'
elif 'Available for Pre-order' in new_stock_count:
new_restock_date = new_stock_count.split('on')[-1].strip()
new_stock_count = 0
new_stock_status = 'pre-order'
elif 'In stock on' in new_stock_count:
new_restock_date = new_stock_count.split('on')[-1].strip()
new_stock_count = 0
new_stock_status = 'replenishing'
elif not new_stock_count:
new_stock_count = 'in stock'
# print new_stock_count, new_stock_status, new_restock_date
# print title, asin, pubdate, author, item_format, price_rent, price, low_price, offer_count, other1format, other1asin, other2format, other2asin, other3format, other3asin, newer, tradein
today_date = str(datetime.now())
scraperwiki.sqlite.save(unique_keys=['Date'], data={'SearchString': unicode(search_term), 'Search Tag': search_tag, 'Title': unicode(title), 'ASIN': asin, 'PubDate': pubdate, 'Author': unicode(author), 'Format': unicode(item_format), 'PriceRent': price_rent, 'Price': price, 'PriceLow': low_price, 'OfferCount': offer_count, 'Other1Format': unicode(other1format), 'Other1ASIN': other1asin, 'Other2Format': unicode(other2format), 'Other2ASIN': other2asin, 'Other3Format': unicode(other3format), 'Other3ASIN': other3asin, 'Newer_Edition': newer, 'TradeIn': unicode(tradein), 'NewStockCount': new_stock_count, 'NewStockStatus': new_stock_status, 'NewRestockDate': new_restock_date, 'Date': today_date})
if __name__ == '__main__':
file1 = open('amazon.txt', 'r')
p = 1
for line in file1:
search_term = line.split('%')[-1].replace('&', '%26')
search_tag = line.split('%')[0]
search_term = '+'.join(search_term.split(' '))
parse(search_term, search_tag, p)
p +=1