Subversion Repositories SmartDukaan

Rev

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

'''
Created on 06-Jun-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.DataAccessor import *
from datastore.DataCodeAccessor import *
from html2text import *
import urllib

class babuchak2(BaseSpider):
    """
    Documentation for class babuchak2
    This spider collects the url for the individual phones
    and store them in table datastore_datadefinition_babuchak_phoneurls.
    """
    def __init__(self):
        """
        Documentation for constructor
        initialize_table is called to make all the tables known in
        the scope of this class.
        Also start url needs to be feeded to the spider through start_urls.append
        Domainname1 is name by which this spider is known outside
        So this will be used as an argument for calling this spider 
        """
        initialize_table()
        #BABUCHAK_DOMAINNAME1 = "babuchak1"   
        BABUCHAK_DOMAINNAME1 = get_code_word("BABUCHAK_DOMAINNAME1")
        self.domain_name = BABUCHAK_DOMAINNAME1 
        #BABUCHAK_VAR1 = "&postPage=" 
        BABUCHAK_VAR1 = get_code_word("BABUCHAK_VAR1")
        da = DataHelper()
        for item in da.get_allbabuchakurls():
            ct = item.no_pages
            while ct>0:
                url = item.url + BABUCHAK_VAR1  
                url = url + str(ct)
                self.start_urls.append(url)
                ct = ct -1        

    def start_requests(self):
        """
        Documentation for method start_requests
        To set various properties of the request to be made
        like referer, headers and all.
        @return a list of well formed requests which will be 
        crawled by spider and spider will return the response
        """
        listreq = []        
        #for each request a referer has to be set
        #BABUCHAK_REFERER = "www.google.com/search"
        BABUCHAK_REFERER = get_code_word("BABUCHAK_REFERER")
        for url1 in self.start_urls:
            request = Request(url = str(url1), callback=self.parse)
            request.headers.setdefault("Referer", BABUCHAK_REFERER)
            listreq.append(request)
        return listreq
        
    def parse(self, response):
        """
        Documentation for method parse
        @param response of individual requests
        Using Xpaths needed information is extracted out of the response
        and added to the database
        Xpath4 = Give us url for individual phone
        Url2 = To get full url for individual vendors
        """
        da = DataHelper()
        #BABUCHAK_URL2 = "http://www.shopping.babuchak.com/visitourstores.php"
        BABUCHAK_URL2 = get_code_word("BABUCHAK_URL2")
        hxs = HtmlXPathSelector(response)
        #BABUCHAK_XPATH4 = '//td[@class="mod-item-body-title"]/a/@href'
        BABUCHAK_XPATH4 = get_code_word("BABUCHAK_XPATH4")
        
        info = hxs.select(BABUCHAK_XPATH4)
        for i in info:
            url = i.extract()
            url = url.strip()
            url = BABUCHAK_URL2 + url 
            da.add_babuchakphoneurl(url) 
       
SPIDER = babuchak2()