-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcurtisandhayes_spider.py
127 lines (114 loc) · 6.04 KB
/
curtisandhayes_spider.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
import scrapy
import csv
import json
import re
from html.parser import HTMLParser
def remove_non_ascii(text):
return ''.join([i if ord(i) < 128 else '' for i in text])
class MLStripper(HTMLParser):
def __init__(self):
self.reset()
self.strict = False
self.convert_charrefs= True
self.fed = []
def handle_data(self, d):
self.fed.append(d)
def get_data(self):
return ''.join(self.fed)
def strip_tags(html):
s = MLStripper()
s.feed(html)
return s.get_data()
class HCVID186_www_curtisandhayes_com_Spider(scrapy.Spider):
name = "HCVID186.www.curtisandhayes.com"
jsonData = []
def start_requests(self):
for x in range(1,4):
link = 'https://www.curtisandhayes.com/chairs.html?limit=36&p='+str(x)
request = scrapy.Request(url=link, callback = self.parse)
request.meta['prod_category'] = 'Chairs'
yield request
for x in range(1,3):
link = 'https://www.curtisandhayes.com/sofas.html?limit=36&p='+str(x)
request = scrapy.Request(url=link, callback = self.parse)
request.meta['prod_category'] = 'Sofas'
yield request
for x in range(1,4):
link = 'https://www.curtisandhayes.com/tables.html?limit=36&p='+str(x)
request = scrapy.Request(url=link, callback = self.parse)
request.meta['prod_category'] = 'Tables'
yield request
for x in range(1,3):
link = 'https://www.curtisandhayes.com/storage.html?limit=36&p='+str(x)
request = scrapy.Request(url=link, callback = self.parse)
request.meta['prod_category'] = 'Storage'
yield request
for x in range(1,3):
link = 'https://www.curtisandhayes.com/lighting.html?limit=36&p='+str(x)
request = scrapy.Request(url=link, callback = self.parse)
request.meta['prod_category'] = 'Lightning'
yield request
for x in range(1,3):
link = 'https://www.curtisandhayes.com/home-decor.html?limit=36&p='+str(x)
request = scrapy.Request(url=link, callback = self.parse)
request.meta['prod_category'] = 'Home Decor'
yield request
for x in range(1,8):
link = 'https://www.curtisandhayes.com/home-furnishings.html?limit=36&p='+str(x)
request = scrapy.Request(url=link, callback = self.parse)
request.meta['prod_category'] = 'Home Furnishings'
yield request
link = 'https://www.curtisandhayes.com/bedroom.html?limit=36'
request = scrapy.Request(url=link, callback = self.parse)
request.meta['prod_category'] = 'Bedroom'
yield request
link = 'https://www.curtisandhayes.com/wardrobes.html?limit=36'
request = scrapy.Request(url=link, callback = self.parse)
request.meta['prod_category'] = 'Wardrobes'
yield request
def parse(self, response):
items = response.css('div.item')
for item in items:
prod_link = item.css('h3.product-name a::attr(href)').extract_first()
prod_name = item.css('h3.product-name a::text').extract_first()
request = scrapy.Request(url=prod_link, callback = self.parseProduct)
request.meta['prod_link'] = prod_link
request.meta['prod_name'] = prod_name
request.meta['prod_category'] = response.meta['prod_category']
yield request
def closed(self, reason):
toWrite = {'data' : HCVID186_www_curtisandhayes_com_Spider.jsonData}
with open('HCVID186.www.curtisandhayes.com.json', 'w+') as outfile:
json.dump(toWrite, outfile, ensure_ascii=False)
def parseProduct(self, response):
productLink = response.meta['prod_link']
productName = response.meta['prod_name']
productPrice = response.css('span.regular-price span.price::text').extract_first().replace('Rs.','').strip()
productImage = response.css('img[itemprop=image]::attr(data-src)').extract_first()
productCategory = response.meta['prod_category']
productDesc = ''
try:
productDesc = response.css('div[itemprop=description]::text').extract_first() + '|'
except TypeError:
pass
descParts = response.css('div.hor_1 div div.std').extract_first()
productDesc += re.sub('<[^>]*>', '', descParts)
productSku = 'SKU-NA'
productCategory = productCategory.replace('"','``')
productDesc = re.sub('\n' ,'|',productDesc)
productDesc = re.sub('\t','',productDesc)
productDesc = re.sub('\r','',productDesc)
productDesc = re.sub('[ ]+',' ',productDesc)
productDesc = re.sub('(\| )+','|',productDesc)
productDesc = re.sub('\|+','|',productDesc)
productDesc = productDesc.strip('|')
productDesc = productDesc.replace('"','``')
productName = productName.replace('"','``')
productDesc = remove_non_ascii(productDesc)
productCategory = remove_non_ascii(productCategory)
productName = remove_non_ascii(productName)
with open('HCVID186.www.curtisandhayes.com.csv', 'a', newline='') as csvfile:
fieldnames = ['Title', 'Category', 'URL', 'IMG_SRC', 'Merchant', 'Price', 'SKU', 'Description']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writerow({'Title': '"'+str(productName)+'"', 'IMG_SRC': '"'+str(productImage)+'"', 'Description': '"'+str(productDesc)+'"','Price': '"'+str(productPrice)+'"', 'URL': '"'+str(productLink)+'"', 'Category': '"'+str(productCategory)+'"', 'SKU': '"'+str(productSku)+'"', 'Merchant': '"NA"'})
HCVID186_www_curtisandhayes_com_Spider.jsonData.append({'Title': productName, 'Description': productDesc, 'IMG_SRC': productImage, 'Price': productPrice, 'SKU': productSku, 'Category': productCategory, 'URL': productLink, 'Merchant': 'NA'})