-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapdeal_csv_scrap_1.py
225 lines (179 loc) · 7.75 KB
/
snapdeal_csv_scrap_1.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#####IMPORING LIBRARIES######
'''
sudo apt-get install python3-pip
sudo pip3 install urllib3
sudo apt-get install python3-bs4
sudo pip3 install pymongo
#importing data into mongodb server#
mongoimport --db production --collection products --type csv --headerline --file /var/data/products.csv
mongod --repair # for reparing the mongo db server
mongod --fork --logpath /var/log/mongodb.log
sudo rm /var/lib/mongodb/mongod.lock #remove lock file
mongod --repair #repair mongo
sudo service mongodb start #restart service
mongo production --eval "db.dropDatabase();" # drop database
'''
#####IMPORING LIBRARIES######
#imporing files starts
#connect multiple sockets connections
import urllib3
#creates pool for multi process
import multiprocessing as mp
#make HTML into a structure to parse
from bs4 import BeautifulSoup
#execute CSV import and export
import csv
#Basic System Commands
import sys
#Execute JSON data
import json
#Use time library
import time
#Mongo database connection library
#import pymongo
#from pymongo import MongoClient
#imporing Files ends here
#set up connection pool using socket
http = urllib3.PoolManager()
#########DEFINING MONGO DB CONNECTION VALUES################
'''
HOST = 'localhost'
PORT = 27017
DATABASE = 'production'
COLLECTION = 'products'
#########DEFINING MONGO DB CONNECTION VALUES################
client = MongoClient()
client = MongoClient(HOST,PORT)
db = client.DATABASE
collection = db.COLLECTION
for data in db.collection.find().limit(2):
print(data)
sys.exit()
'''
BASE_URL = 'http://www.snapdeal.com/'
PIN_CODE = '110001'
AFFILIATE_ID = '?utm_source=aff_prog&utm_campaign=afts&offer_id=17&aff_id=8121'
headers = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding':'gzip, deflate',
'Accept-Language':'en-US,en;q=0.5',
'Connection':'keep-alive',
'Host':'www.snapdeal.com',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0'
}
# call this method to execut the call of each product URL's#
def callEachProductsWithURL(url):
r = http.request('GET',url,headers=headers)
print(r.status)
if(r.status == 200):
time_stmp = time.strftime('%Y-%m-%d %H:%M:%S')
bread_crumb = ''
product_name= ''
product_description= ''
mrp= ''
selling_price= ''
in_stock= ''
product_id= ''
brandName= ''
codValidOnCategory= ''
shippingCharges= ''
shippingDays= ''
product_url= ''
image_url= ''
soup = BeautifulSoup(r.data.decode('utf-8'),"lxml")
mainList = {}
bc_list = list()
if soup.find('div',{'id':'breadCrumbWrapper2'}) is not None:
bread_crumb = soup.find('div',{'id':'breadCrumbWrapper2'})
for crumbs in bread_crumb.find_all('a',{'class','bCrumbOmniTrack'}):
cc = crumbs.text
cc = cc.strip()
bc_list.append(cc)
bread_crumb = " > ".join(bc_list)
if soup.find('input',{'id':'productNamePDP'}).attrs['value'] is not None:
product_name = soup.find('input',{'id':'productNamePDP'}).attrs['value'].strip()
if soup.find('div',{'class':'detailssubbox'}) is not None:
product_description = soup.find('div',{'class':'detailssubbox'}).text.strip()
if soup.find('input',{'id':'productPrice'}).attrs['value'] is not None:
selling_price = soup.find('input',{'id':'productPrice'}).attrs['value'].strip()
if soup.find('input',{'id':'soldOut'}).attrs['value'] is not None:
in_stock = soup.find('input',{'id':'soldOut'}).attrs['value'].strip()
if in_stock == 'false':
if soup.find('input',{'id':'productMrpForFbt'}) is not None:
mrp = soup.find('input',{'id':'productMrpForFbt'}).attrs['value'].strip()
else:
mrp = selling_price
#CALLING SHIPPING DETAILS
supc = soup.find('div',{'id':'defaultSupc'}).text.strip()
catId = soup.find('input',{'id':'catId'}).attrs['value'].strip()
catUrl = soup.find('input',{'id':'catUrl'}).attrs['value'].strip()
bn = soup.find('input',{'id':'brandName'}).attrs['value'].strip()
url_shipping = BASE_URL+'/acors/json/v2/gvbps?supc='+supc+'&catId='+catId+'&pc='+PIN_CODE+'&catUrl='+catUrl+'&bn='+bn+'&start=0&count=1'
url_shipping = re.sub(' ', '%20', url_shipping)
r = http.request('GET',url_shipping,headers=headers)
data = json.loads(r.data.decode('utf-8'))
try:
shippingCharges = str(data['primaryVendor']['shippingCharges'])
shippingDays = str(data['primaryVendor']['otoDRange']['min'])+' to '+str(data['primaryVendor']['otoDRange']['max'])+' Days'
except KeyError:
shippingCharges = 0
shippingDays = ''
else:
mrp = selling_price
shippingCharges = 0
shippingDays = ''
#average_rating = soup.find('input',{'id':'avgRating'}).attrs['value'].strip()
#no_of_rating = soup.find('input',{'id':'noOfRatings'}).attrs['value'].strip()
if(in_stock =='false'):
in_stock = 1
else:
in_stock = 0
if soup.find('input',{'id':'pogId'}).attrs['value'] is not None:
product_id = soup.find('input',{'id':'pogId'}).attrs['value'].strip()
if soup.find('input',{'id':'brandName'}).attrs['value'] is not None:
brandName = soup.find('input',{'id':'brandName'}).attrs['value'].strip()
if soup.find('input',{'id':'codValidOnCategory'}).attrs['value'] is not None:
codValidOnCategory = soup.find('input',{'id':'codValidOnCategory'}).attrs['value'].strip()
if soup.find('input',{'id':'productPageUrl'}).attrs['value'] is not None:
product_url = BASE_URL+soup.find('input',{'id':'productPageUrl'}).attrs['value'].strip()
product_url = product_url+AFFILIATE_ID
if soup.find('div',{'class','left-panel-carousel'}) is not None:
image_url_div = soup.find('div',{'class','left-panel-carousel'})
image_url = image_url_div.find_all('img')
image_url = image_url[0].attrs['src'].strip()
print(product_id)
mainList['bread_crumb'] = str(bread_crumb)
mainList['product_name'] = str(product_name)
mainList['product_description'] = str(product_description)
mainList['mrp'] = str(mrp)
mainList['selling_price'] = str(selling_price)
mainList['in_stock'] = str(in_stock)
mainList['product_id'] = str(product_id)
mainList['brandName'] = str(brandName)
mainList['codValidOnCategory'] = str(codValidOnCategory)
mainList['shippingCharges'] = str(shippingCharges)
mainList['shippingDays'] = str(shippingDays)
mainList['product_url'] = str(product_url)
mainList['image_url'] = str(image_url)
mainList['time_stmp'] = str(time_stmp)
if product_id :
storeDataIntoDB(mainList,product_id)
def storeDataIntoDB(mainList,product_id):
print(mainList)
# call this method at the initial time for fetching data from the mysql#
def callInitProducts():
if __name__ == '__main__':
pool = mp.Pool()
with open('/snapdir/' + sys.argv[1], 'r') as csvfile:
urls = csv.reader(csvfile, delimiter=' ', quotechar='|')
for url in urls:
try:
url_name = url[0]
print(url_name)
pool.apply_async(callEachProductsWithURL, args=(url_name,))
except Exception as inst:
print(inst)
pool.close()
pool.join()
print('end')
callInitProducts()