Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
185 ashish 1
'''
2
Created on 20-May-2010
3
 
4
@author: gaurav
5
'''
6
 
7
from scrapy.spider import BaseSpider
8
from scrapy.selector import HtmlXPathSelector
9
from scrapy.http import Request
10
 
11
from demo.items import DemoItem
12
from scrapy.contrib.spidermiddleware import referer
13
from scrapy.http.headers import Headers
14
from scrapy.http.request.form import FormRequest
15
from scrapy.log import msg
16
from scrapy.http.response import Response
17
 
18
 
19
from datastore import DataAccessor
20
from datastore.DataAccessor import DataHelper
234 ashish 21
from html2text.unescaping import *
185 ashish 22
 
23
class mobstore_price(BaseSpider):
24
 
25
    def __init__(self):
234 ashish 26
       MOBILESTORE_DOMAINNAME1 = "mobilestore1"   
27
       self.domain_name = MOBILESTORE_DOMAINNAME1 
28
       # get urls from the database and append them in the list for crawling
185 ashish 29
       da = DataHelper()
30
       for pitem in da.get_allmobstoreurls():
31
            self.start_urls.append(pitem.url.strip())
32
 
33
    def start_requests(self):
34
        listreq = []
234 ashish 35
        #for each request a referer has to be set
36
        MOBILESTORE_REFERER = "www.google.com/search"
185 ashish 37
        for url1 in self.start_urls:
234 ashish 38
            request = Request(url = str(url1), callback=self.parse)
39
            request.headers.setdefault("Referer", MOBILESTORE_REFERER)
185 ashish 40
            listreq.append(request)
41
        return listreq
234 ashish 42
 
185 ashish 43
    def parse(self, response):
44
        site = response.url
234 ashish 45
        site = unescape(site)
46
        MOBILESTORE_VATPLUSTAX = 0
47
 
48
        #retreiving model-name from the url
185 ashish 49
        pos1 = pos2 = 0
50
        temp = ""
51
        pos1 = site.rfind('/')
52
        if pos1 != -1:
53
            temp = site[pos1+1:len(site)]
54
        pos3 = temp.find('.')
55
        temp1 = temp[pos3:len(temp)]
56
        name = temp.replace(temp1,"")         
57
        hxs = HtmlXPathSelector(response)
234 ashish 58
        MOBILESTORE_XPATH2 = '//div[@id ="priceComp"]//tr[2]/td[3]/span/text()' 
59
        prices = hxs.select(MOBILESTORE_XPATH2)
185 ashish 60
 
234 ashish 61
        #removelist is used for converting price to decimal format containing only numbers and '.'
62
        MOBILESTORE_REMOVELIST = ["Rs",",","-","/","Rs."]
185 ashish 63
        da = DataHelper()
64
        for price in prices:
65
             name = str(name).strip()
66
             price = price.extract()
67
             price = str(price).strip()
234 ashish 68
             if price != '':        
69
                for r in MOBILESTORE_REMOVELIST: 
70
                    while price.find(r) != -1:
71
                        price = price.replace(r, "")
72
             price = price.strip()
185 ashish 73
             shown_pr = int(price)
234 ashish 74
             final_pr = shown_pr + MOBILESTORE_VATPLUSTAX 
185 ashish 75
             da.add_new_mobstorephone(name,shown_pr,final_pr)
76
SPIDER = mobstore_price()
77