-
Notifications
You must be signed in to change notification settings - Fork 1
/
ScrapingAmazon.py
163 lines (145 loc) · 6.4 KB
/
ScrapingAmazon.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
import requests
from bs4 import BeautifulSoup
from fake_headers import Headers
import json
import csv
import re
import time
import random
proxies = []
class ScrapingAmazon():
def __init__(self, keywords):
self.session = requests.Session()
self.url = f"https://www.amazon.co.uk/s?k={keywords.replace(' ', '+')}"
self.products_asin = []
def get_Asins(self):
current_page = self.url
while True:
try:
response = self.session.get(
current_page, headers=Headers(headers=True).generate())
soup = BeautifulSoup(response.text, "lxml")
items_box = soup.find("div", class_='s-search-results')
#items = items_box.findAll("div", {"data-asin" : re.compile(r".*")})
#items = items_box.findAll("div", {"data-asin" : True})
items = items_box.find_all('div', recursive=False)
for item in items:
if len(item['data-asin']):
self.products_asin.append(item['data-asin'])
except Exception as ex:
pass
next_page = soup.find('a', class_='s-pagination-next')
if(next_page):
current_page = self.url + next_page['href']
print(current_page)
else:
break
def set_AsinFile(self, fileName="Asins.txt"):
with open(fileName, 'w') as file:
for asin in self.products_asin:
file.write(asin + '\n')
def get_AsinsFile(self, fileName="Asins.txt"):
with open(fileName, 'r') as file:
self.products_asin.extend([p.rstrip() for p in file])
def productInfo(self, asin):
url = f'https://www.amazon.co.uk/dp/{asin}'
while True:
proxy = {'http': random.choice(proxies)}
response = requests.get(
url, proxies=proxy, headers=Headers(headers=True).generate())
if(response.status_code == 200):
break # Break Loop, this is successful Requests
soup = BeautifulSoup(response.text, 'lxml')
try:
product_info = {
'Asin': asin,
'Title': soup.find('span', id="productTitle").string.strip(),
'Date': soup.find('table', id="productDetails_detailBullets_sections1").find_all('tr')[-1].td.text.strip()
# 'reviews': self.productReview(asin) ## B0B6G5BGDR
}
#########################################################
try:
price = soup.find('div', id="corePrice_feature_div").div.span.find(
'span', class_="a-offscreen")
price = price.text.replace('£', '').replace(',', '')
product_info['Price'] = price
except:
product_info['Price'] = None
#########################################################
try:
product_info['Sold by'] = soup.find(
'a', id="sellerProfileTriggerId").string
except:
product_info['Sold by'] = "Amazon"
#########################################################
try:
product_info['Review Count'] = soup.find(
'div', {'data-hook': "total-review-count"}).span.text.strip().replace(' global rating', '').replace('s', '')
product_info['Rating'] = soup.find(
'span', {'data-hook': "rating-out-of-text"}).string.replace(' out of 5', '')
except:
product_info['Review Count'] = "0"
product_info['Rating'] = "0"
print(product_info)
return product_info
except:
return # Weak Product
def productReview(self, asin):
url = f'https://www.amazon.co.uk/product-reviews/{asin}'
try:
while True:
proxy = {'http': random.choice(proxies)}
response = requests.get(url, proxies=proxy,
headers=Headers(headers=True).generate())
if(response.status_code == 200):
break
soup = BeautifulSoup(response.text, 'lxml')
reviews = soup.find_all('div', {'data-hook': "review"})
product_reviews = []
for review in reviews:
product_review = {
'Rating': review.find('a', class_="a-link-normal").string.replace(" out of 5 stars", ""),
'Review Title': review.find('a', {'data-hook': "review-title"}).span.string
}
product_reviews.append(product_review)
print(product_reviews)
return product_reviews
except Exception as ex:
return []
def to_Json(self, fileName='Products.json'):
products = []
# Change Format
for asin in set(self.products_asin):
products.append({
asin: {
'Details': self.productInfo(asin),
'Review': self.productReview(asin)
}
})
with open(fileName, 'w') as file:
json.dump(products, file, indent=4)
def to_CSV(self, fileName='Products.csv'):
with open(fileName, 'w', newline='') as file:
## CSV_file = csv.writer(file, delimiter=',')
writer = csv.DictWriter(file,
fieldnames=['Asin', 'Title', 'Price', 'Sold by', 'Review Count', 'Rating', 'Date'])
writer.writeheader()
for asin in set(self.products_asin):
p = self.productInfo(asin)
## CSV_file.writerow([asin, p['Title'], p['Price'], p['Sold by'], p['Review Count'], p['Rating']])
if(p):
writer.writerow(p)
def __del__(self):
self.session.close()
def get_proxies(fileName="proxies.txt"):
with open(fileName, 'r') as file:
proxies.extend([p.rstrip() for p in file])
############################################################
startTime = time.time()
get_proxies()
amazon = ScrapingAmazon('laptop')
# amazon.get_Asins()
# amazon.set_AsinFile()
amazon.get_AsinsFile()
amazon.to_CSV()
print(f'Execution Time: {(time.time() - startTime):.3f} sec')