-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbandhini_home.py
55 lines (40 loc) · 1.75 KB
/
bandhini_home.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
import scrapy
import logging
from scrapy.contrib.spiders import Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from scrapy import Request, Spider
from scrapy.exceptions import CloseSpider
from scrapy.selector import Selector
from scrapy.item import Item, Field
class ScrapySampleItem(Item):
title = Field()
link = Field()
desc = Field()
price = Field()
image = Field()
class StackOverflowSpider(scrapy.Spider):
name = 'bandhini home'
start_urls = ["http://www.bandhinihome.com/product-types/cushions.html"]
def parse(self, response):
for href in response.css('.product-image::attr(href)'):
full_url = response.urljoin(href.extract())
yield scrapy.Request(full_url, callback=self.parse_product)
next_page = response.css(".next::attr('href')")
if next_page:
url = response.urljoin(next_page[0].extract())
yield scrapy.Request(url, self.parse)
def parse_product(self, response):
items = []
item = ScrapySampleItem()
item['title'] = response.css('h1::text').extract_first()
item['image'] = response.css('.item img::attr(src)').extract_first()
item['desc'] = response.css('.short-description').extract()
item['price'] = response.css('.price-box span span::text').extract_first()
if not item['desc']:
logging.info("EMPTY RECIEVED")
item['desc'] = response.css('.rte').extract()
item['link'] = response.url
items.append(item)
for item in items:
yield item