Subversion Repositories SmartDukaan

Rev

Rev 185 | Blame | Compare with Previous | Last modification | View Log | RSS feed

'''
Created on 20-May-2010

@author: gaurav
'''

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from scrapy.http import Request

from demo.items import DemoItem
from scrapy.contrib.spidermiddleware import referer
from scrapy.http.headers import Headers
from scrapy.http.request.form import FormRequest
from scrapy.log import msg
from scrapy.http.response import Response


from datastore import DataAccessor
from datastore.DataAccessor import DataHelper
from html2text.unescaping import *

class mobstore_price(BaseSpider):
    
    def __init__(self):
       MOBILESTORE_DOMAINNAME1 = "mobilestore1"   
       self.domain_name = MOBILESTORE_DOMAINNAME1 
       # get urls from the database and append them in the list for crawling
       da = DataHelper()
       for pitem in da.get_allmobstoreurls():
            self.start_urls.append(pitem.url.strip())
    
    def start_requests(self):
        listreq = []
        #for each request a referer has to be set
        MOBILESTORE_REFERER = "www.google.com/search"
        for url1 in self.start_urls:
            request = Request(url = str(url1), callback=self.parse)
            request.headers.setdefault("Referer", MOBILESTORE_REFERER)
            listreq.append(request)
        return listreq
        
    def parse(self, response):
        site = response.url
        site = unescape(site)
        MOBILESTORE_VATPLUSTAX = 0
        
        #retreiving model-name from the url
        pos1 = pos2 = 0
        temp = ""
        pos1 = site.rfind('/')
        if pos1 != -1:
            temp = site[pos1+1:len(site)]
        pos3 = temp.find('.')
        temp1 = temp[pos3:len(temp)]
        name = temp.replace(temp1,"")         
        hxs = HtmlXPathSelector(response)
        MOBILESTORE_XPATH2 = '//div[@id ="priceComp"]//tr[2]/td[3]/span/text()' 
        prices = hxs.select(MOBILESTORE_XPATH2)
        
        #removelist is used for converting price to decimal format containing only numbers and '.'
        MOBILESTORE_REMOVELIST = ["Rs",",","-","/","Rs."]
        da = DataHelper()
        for price in prices:
             name = str(name).strip()
             price = price.extract()
             price = str(price).strip()
             if price != '':        
                for r in MOBILESTORE_REMOVELIST: 
                    while price.find(r) != -1:
                        price = price.replace(r, "")
             price = price.strip()
             shown_pr = int(price)
             final_pr = shown_pr + MOBILESTORE_VATPLUSTAX 
             da.add_new_mobstorephone(name,shown_pr,final_pr)
SPIDER = mobstore_price()