forked from theriley106/AmazonTextbookArbitrage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextbookAPI.py
More file actions
222 lines (208 loc) · 6.22 KB
/
TextbookAPI.py
File metadata and controls
222 lines (208 loc) · 6.22 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
import threading
import random
import requests
import RandomHeaders
import bs4
import time
import csv
proxies = {
}
def get_dec(x):
a = (float(''.join(ele for ele in x if ele.isdigit() or ele == '.')))
a = float("{0:.2f}".format(a))
return a
def AmazonTrade(Keywords, CSV=False, Debug=0):
Information = []
URLs = []
for keyword in Keywords:
for i in range(1,75):
URLs.append('https://www.amazon.com/s/ref=sr_pg_{}?srs=9187220011&fst=as%3Aoff&rh=n%3A283155%2Ck%3Atextbooks&page={}&keywords={}'.format(i, i, str(keyword)))
def NewURL(URLs):
try:
url = random.choice(URLs)
URLs.remove(url)
return str(url)
except BaseException as exp:
if Debug > 1:
print(exp)
pass
def TradeFromHTML(colorprice):
a = (float(''.join(ele for ele in colorprice if ele.isdigit() or ele == '.')))
a = float("{0:.2f}".format(a))
return a
def DownloadPage(URLs):
URL = NewURL(URLs)
for tries in range(3):
try:
res = requests.get(URL, headers=RandomHeaders.LoadHeader(), proxies=proxies)
print(URL)
page = bs4.BeautifulSoup(res.text, "lxml")
break
except BaseException as exp:
if Debug > 1:
print(exp)
pass
return page
def ConvertPageToNumbers(page):
items = page.select('.s-item-container')
for item in items:
TIV = TradeFromHTML(str(item.select('.a-color-price')))
item = str(item).partition('ISBN-10:')[2]
item = str(item).partition('left;"><span class="a-size-small a-color-secondary">')[2]
ASIN = str(item).partition('</span></div>')[0]
Information.append([ASIN, TIV])
def RemoveDuplicate(rawlist):
#2 Part List Only
NewList = []
for Contents in rawlist:
if Contents not in NewList:
NewList.append(Contents)
return NewList
def RemoveCheapBooks(rawlist, price):
NewerList = []
for Contents in rawlist:
if Contents[1] > price:
NewerList.append(Contents)
return NewerList
def Scrape(URLs):
while len(URLs) > 0:
try:
Page = DownloadPage(URLs)
ConvertPageToNumbers(Page)
except BaseException as exp:
if Debug > 1:
print(exp)
pass
threads = [threading.Thread(name='worker{}'.format(n), target=Scrape, args=([URLs])) for n in range(10)]
for t in threads: t.start()
for t in threads: t.join()
Information = RemoveDuplicate(Information)
Information = RemoveCheapBooks(Information, 50)
if CSV != False:
with open(CSV, "wb") as f:
writer = csv.writer(f)
writer.writerows(Information)
return Information
def Biblio(ListOfASIN, CSV=False, Debug=0):
Information = []
def TradeFromHTML(colorprice):
a = (float(''.join(ele for ele in colorprice if ele.isdigit() or ele == '.')))
a = float("{0:.2f}".format(a))
return a
def GenerateURL(ListOfASIN):
try:
Asin = random.choice(ListOfASIN)
Url = 'http://www.biblio.com/search.php?stage=1&pageper=50&country=1&omit_product_types=bp,bd,ns&keyisbn={}&strip_common=1&program=1005&order=price_ship_usasc'.format(Asin)
ListOfASIN.remove(Asin)
return [str(Url), str(Asin)]
except BaseException as exp:
if Debug > 1:
print(exp)
pass
def DownloadPage(ListOfASIN):
URL = GenerateURL(ListOfASIN)
Asin = URL[1]
URL = URL[0]
for tries in range(3):
try:
res = requests.get(URL, headers=RandomHeaders.LoadHeader(), proxies=proxies)
print(URL)
page = bs4.BeautifulSoup(res.text, "lxml")
break
except BaseException as exp:
if Debug > 1:
print(exp)
pass
return [page, Asin]
def ConvertPageToNumbers(page, Asin):
Price = TradeFromHTML(str(str(page.select('.ob-price')[0]).partition('Price: <span class="price">')[2]).partition(' </span>\n</h3>\n<span class="shipping"')[0])
Shipping = TradeFromHTML(str(str(page.select('.ob-price')[0]).partition('"shipping">Shipping (US): </span>')[2]))
Price = round(Price + Shipping, 2)
Information.append([Asin, Price])
def Scrape(ListOfASIN):
while len(ListOfASIN) > 0:
try:
Data = DownloadPage(ListOfASIN)
page = Data[0]
Asin = Data[1]
ConvertPageToNumbers(page, Asin)
except BaseException as exp:
if Debug > 0:
print(exp)
time.sleep(1)
pass
if hasattr(ListOfASIN, 'lower'):
#Determines if the user inputted a string or list
ListOfASIN = [ListOfASIN]
threads = [threading.Thread(name='worker{}'.format(n), target=Scrape, args=([ListOfASIN])) for n in range(10)]
for t in threads: t.start()
for t in threads: t.join()
if CSV != False:
with open(CSV, "wb") as f:
writer = csv.writer(f)
writer.writerows(Information)
return Information
def Half(ListOfASIN, CSV=False, Debug=0):
Information = []
def TradeFromHTML(colorprice):
a = (float(''.join(ele for ele in colorprice if ele.isdigit() or ele == '.')))
a = float("{0:.2f}".format(a))
return a
def GenerateURL(ListOfASIN):
try:
Asin = random.choice(ListOfASIN)
Url = 'http://search.half.ebay.com/{}'.format(Asin)
ListOfASIN.remove(Asin)
return [str(Url), str(Asin)]
except BaseException as exp:
if Debug > 1:
print(exp)
pass
def DownloadPage(ListOfASIN):
URL = GenerateURL(ListOfASIN)
Asin = URL[1]
URL = URL[0]
for tries in range(3):
try:
res = requests.get(URL, headers=RandomHeaders.LoadHeader(), proxies=proxies)
print(URL)
page = bs4.BeautifulSoup(res.text, "lxml")
break
except BaseException as exp:
if Debug > 1:
print(exp)
pass
return [page, Asin]
def ConvertPageToNumbers(page, Asin):
halfprices = []
re = page.select('.PDP_itemList')
for tables in re:
price = TradeFromHTML(tables.select('.tr-border td')[0].getText())
halfprices.append([price])
halfprices.sort()
Price = halfprices[0]
Price = round((Price[0] + 3.49),2)
Information.append([Asin, Price])
def Scrape(ListOfASIN):
while len(ListOfASIN) > 0:
try:
Data = DownloadPage(ListOfASIN)
page = Data[0]
Asin = Data[1]
ConvertPageToNumbers(page, Asin)
except BaseException as exp:
if Debug > 0:
print(exp)
time.sleep(1)
pass
if hasattr(ListOfASIN, 'lower'):
#Determines if the user inputted a string or list
ListOfASIN = [ListOfASIN]
threads = [threading.Thread(name='worker{}'.format(n), target=Scrape, args=([ListOfASIN])) for n in range(10)]
for t in threads: t.start()
for t in threads: t.join()
if CSV != False:
with open(CSV, "wb") as f:
writer = csv.writer(f)
writer.writerows(Information)
return Information