Subversion Repositories SmartDukaan

Rev

Rev 187 | Rev 261 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
187 ashish 1
'''
2
Created on 27-May-2010
3
 
4
@author: gaurav
5
'''
6
 
7
 
8
from scrapy.spider import BaseSpider
9
from scrapy.selector import HtmlXPathSelector
10
from scrapy.http import Request
11
 
12
from demo.items import DemoItem
13
from scrapy.contrib.spidermiddleware import referer
14
from scrapy.http.headers import Headers
15
from scrapy.http.request.form import FormRequest
16
from scrapy.log import msg
17
from scrapy.http.response import Response
235 ashish 18
from datastore.DataCodeAccessor import *
19
from datastore.DataAccessor import *
187 ashish 20
 
235 ashish 21
from html2text.unescaping import *
187 ashish 22
 
23
class naaptol_spider(BaseSpider):
24
 
235 ashish 25
    def __init__(self):
26
       initialize_table() 
27
       #NAAPTOL_DOMAINNAME = "naaptol"   
28
       NAAPTOL_DOMAINNAME = get_code_word("NAAPTOL_DOMAINNAME")
29
       self.domain_name = NAAPTOL_DOMAINNAME 
30
       #NAAPTOL_URL = "http://www.naaptol.com/sitemap.xml"
31
       NAAPTOL_URL = get_code_word("NAAPTOL_URL")
32
       self.start_urls.append(NAAPTOL_URL)
187 ashish 33
 
34
 
35
    def start_requests(self):
235 ashish 36
        #adding entry for the supplier i.e its name and site
37
        #NAAPTOL_HOMEPAGE = "http://www.naaptol.com"
38
        NAAPTOL_HOMEPAGE = get_code_word("NAAPTOL_HOMEPAGE")
187 ashish 39
        da = DataHelper()
235 ashish 40
        da.add_supplier(self.domain_name, NAAPTOL_HOMEPAGE)
187 ashish 41
        listreq = []
235 ashish 42
 
43
        #for each request a referer has to be set
44
        #NAAPTOL_REFERER = "http://www.google.com"
45
        NAAPTOL_REFERER = get_code_word("NAAPTOL_REFERER")
187 ashish 46
        for url1 in self.start_urls:
47
            request = Request(url = str(url1), callback=self.parse)
235 ashish 48
            request.headers.setdefault("Referer", NAAPTOL_REFERER)
187 ashish 49
            listreq.append(request)
50
        return listreq
51
 
52
    def parse(self, response):
53
        da = DataHelper()
54
        hxs = HtmlXPathSelector(response)
235 ashish 55
        #NAAPTOL_XPATH1 = '//url/loc/text()'
56
        NAAPTOL_XPATH1 = get_code_word("NAAPTOL_XPATH1")
57
        phone_urls = hxs.select(NAAPTOL_XPATH1)
58
 
59
        #elements in chk_list are specific to this site for determining valid sites 
60
        #NAAPTOL_CHKLIST1 = ["mobile_phones/pdas_and_smartphones" ,"mobile_phones/gsm_handsets" ,"mobile_phones/cdma_handsets"]
61
        #list separeated by ';'
62
        NAAPTOL_CHKLIST1 = get_code_word("NAAPTOL_CHKLIST1")
63
        NAAPTOL_CHKLIST1 = NAAPTOL_CHKLIST1.split(';')
187 ashish 64
        for i in phone_urls:
65
            site = i.extract()
235 ashish 66
            site = unescape(site)
187 ashish 67
            pos1 = pos2 = pos3 = 0
68
            temp =""
235 ashish 69
 
70
            # temp contains string b/w 2nd last and 3rd last slash(/)
187 ashish 71
            pos1 = site.rfind('/')
72
            if pos1 != -1:
73
                pos2 = site.rfind('/',0,pos1-1)
74
            if pos2 != -1:    
75
                pos3 = site.rfind('/',0,pos2-1)
76
            if pos3 > 0:
77
                temp = site[pos3+1:pos1]
235 ashish 78
            for c in NAAPTOL_CHKLIST1:
79
                if temp == c:
80
                    da.add_naaptolurl(site)           
187 ashish 81
SPIDER = naaptol_spider()