Subversion Repositories SmartDukaan

Rev

Rev 253 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
223 ashish 1
'''
2
Created on 06-Jun-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
from datastore.DataAccessor import *
19
from datastore.DataCodeAccessor import *
20
from html2text import *
21
from babel.messages.pofile import unescape
22
import urllib
23
 
24
class babuchak3(BaseSpider):
25
 
26
    def __init__(self):
27
        initialize_table()
28
        #BABUCHAK_DOMAINNAME2 = "babuchak2"   
29
        BABUCHAK_DOMAINNAME2 = get_code_word("BABUCHAK_DOMAINNAME2")
30
        self.domain_name = BABUCHAK_DOMAINNAME2  
31
        da = DataHelper()
32
        for item in da.get_allbabuchakphoneurls():
33
            self.start_urls.append(item.url)
34
 
35
    def start_requests(self):
36
        listreq = []
37
        #for each request a referer has to be set
38
        #BABUCHAK_REFERER = "www.google.com/search"
39
        BABUCHAK_REFERER = get_code_word("BABUCHAK_REFERER")
40
        for url1 in self.start_urls:
41
            request = Request(url = str(url1), callback=self.parse)
42
            request.headers.setdefault("Referer", BABUCHAK_REFERER)
43
            listreq.append(request)
44
        return listreq
45
 
46
    def parse(self, response):
47
        #url1 needed to get complete urls
48
        da = DataHelper()
49
        hxs = HtmlXPathSelector(response)
50
        #BABUCHAK_XPATH5 = '//td[@class="text-header"]/text()'
51
        BABUCHAK_XPATH5 = get_code_word("BABUCHAK_XPATH5")
52
        #BABUCHAK_XPATH6 = '//td[@class="xl63"]//strong/span/text()'
53
        BABUCHAK_XPATH6 = get_code_word("BABUCHAK_XPATH6")
54
        #BABUCHAK_XPATH7 = '//td[@class="mod-item-body-title"]/b/text()'
55
        BABUCHAK_XPATH7 = get_code_word("BABUCHAK_XPATH7")
56
        #BABUCHAK_REMOVELIST = ["Rs.","Rs",",","-","/"]
57
        #list separated by ';'
58
        BABUCHAK_REMOVELIST = get_code_word("BABUCHAK_REMOVELIST")
59
        BABUCHAK_REMOVELIST = BABUCHAK_REMOVELIST.split(';')
60
        name = hxs.select(BABUCHAK_XPATH5)[0].extract()
61
        try:
62
            shown_price = hxs.select(BABUCHAK_XPATH6)[0].extract()
63
            final_price = hxs.select(BABUCHAK_XPATH6)[2].extract()
64
        except:
65
            final_price = shown_price = hxs.select(BABUCHAK_XPATH7)[0].extract()
66
 
67
 
68
        name = name.strip()
69
        shown_price = shown_price.strip()
70
        final_price = final_price.strip()
71
 
72
        if shown_price != '':        
73
            for r in BABUCHAK_REMOVELIST: 
74
                while shown_price.find(r) != -1:
75
                    shown_price = shown_price.replace(r, "")
76
        shown_price = shown_price.strip()
77
 
78
        if final_price != '':        
79
            for r in BABUCHAK_REMOVELIST: 
80
                while final_price.find(r) != -1:
81
                    final_price = final_price.replace(r, "")
82
        final_price = final_price.strip()
83
        ps1 = shown_price.find('.')
84
        if ps1 != -1:
85
            shown_price = shown_price[0:ps1]
86
            final_price = shown_price
87
        shown_price = int(shown_price)
88
        final_price = int(final_price)
89
        if shown_price>final_price:
90
            try:
91
                shown_price = hxs.select(BABUCHAK_XPATH6)[1].extract()
92
                final_price = hxs.select(BABUCHAK_XPATH6)[2].extract()
93
            except:
94
                final_price = shown_price = hxs.select(BABUCHAK_XPATH7)[0].extract()
95
 
96
            name = name.strip()
97
            shown_price = shown_price.strip()
98
            final_price = final_price.strip()
99
 
100
            if shown_price != '':        
101
                for r in BABUCHAK_REMOVELIST: 
102
                    while shown_price.find(r) != -1:
103
                        shown_price = shown_price.replace(r, "")
104
            shown_price = shown_price.strip()
105
 
106
            if final_price != '':        
107
                for r in BABUCHAK_REMOVELIST: 
108
                    while final_price.find(r) != -1:
109
                        final_price = final_price.replace(r, "")
110
            final_price = final_price.strip()
111
            ps1 = shown_price.find('.')
112
            if ps1 != -1:
113
                shown_price = shown_price[0:ps1]
114
                final_price = shown_price
115
 
116
            shown_price = int(shown_price)
117
            final_price = int(final_price)
118
 
119
        print name
120
        print shown_price
121
        print final_price
122
        da.add_babuchakphone(name,shown_price,final_price)        
123
SPIDER = babuchak3()