Rev 281 | Blame | Compare with Previous | Last modification | View Log | RSS feed
'''Created on 13-May-2010@author: gaurav'''from scrapy.spider import BaseSpiderfrom scrapy.selector import HtmlXPathSelectorfrom scrapy.http import Requestfrom demo.items import DemoItemfrom scrapy.contrib.spidermiddleware import refererfrom scrapy.http.headers import Headersfrom scrapy.http.request.form import FormRequestfrom scrapy.log import msgfrom scrapy.http.response import Responsefrom datastore.DataAccessor import *from datastore.DataCodeAccessor import *from html2text.unescaping import *from elixir import *class infi_spider(BaseSpider):"""Documentation for class infi_spiderThis spider collects the information for the individual phonesand store them in table datastore_datadefinition_infibeam_data"""def __init__(self):"""Documentation for constructorinitialize_table is called to make all the tables known inthe scope of this class.Also start url needs to be feeded to the spider through start_urls.appendDomainname is name by which this spider is known outsideSo this will be used as an argument for calling this spider.As the number of pages to be crawled are not fixed so ct and no are used to make it dynamic."""initialize_table()da = DataHelper()#INFIBEAM_DOMAINNAME = "infibeam"INFIBEAM_DOMAINNAME = get_code_word("INFIBEAM_DOMAINNAME")print INFIBEAM_DOMAINNAMEself.domain_name = INFIBEAM_DOMAINNAME#INFIBEAM_CT = 15#INFIBEAM_CT = int(get_code_word("INFIBEAM_CT"))#ct = 15ct = int(da.get_extra_vars('infibeam_count'))#INFIBEAM_NO = 1no = 1if ct>14:no = ct#INFIBEAM_URL = "http://www.infibeam.com/Mobiles/search?page="INFIBEAM_URL = get_code_word("INFIBEAM_URL")while(no<=ct):url1 = INFIBEAM_URL + str(no)self.start_urls.append(url1)no=no+1def start_requests(self):"""Documentation for method start_requestsTo set various properties of the request to be madelike referer, headers and all.Also suppliers entry need to be done in the tabledatastore_datadefinition_suppliers.@return a list of well formed requests which will becrawled by spider and spider will return the response"""#adding entry for the supplier i.e its name and site#INFIBEAM_HOMEPAGE = "www.infibeam.com"INFIBEAM_HOMEPAGE = get_code_word("INFIBEAM_HOMEPAGE")da = DataHelper()da.add_supplier(self.domain_name, INFIBEAM_HOMEPAGE)listreq = []#for each request a referer has to be set#INFIBEAM_REFERER = "www.google.com/search"INFIBEAM_REFERER = get_code_word("INFIBEAM_REFERER")for url1 in self.start_urls:request = Request(url = str(url1), callback=self.parse, dont_filter=True)request.headers.setdefault("Referer", INFIBEAM_REFERER)listreq.append(request)return listreqdef parse(self, response):"""Documentation for method parse@param response of individual requestsUsing Xpaths needed information is extracted out of the responseand added to the databaseXpath1 = Give us section for individual phoneXpath2 = Give us name for individual phoneXpath3 = Give us quoted price for individual phonevatplustax = to get final price from quoted priceRemovelist = To filer the prices so as to make them integer for eg remove ',' or 'Rs'"""da = DataHelper()#INFIBEAM_VATPLUSTAX = 0INFIBEAM_VATPLUSTAX = int(get_code_word("INFIBEAM_VATPLUSTAX"))#list elements are separated by ';'#INFIBEAM_REMOVELIST = ["Rs.",",","-","/","Rs"]INFIBEAM_REMOVELIST = str(get_code_word("INFIBEAM_REMOVELIST"))if len(INFIBEAM_REMOVELIST)>0:INFIBEAM_REMOVELIST = INFIBEAM_REMOVELIST.split(';')for x in INFIBEAM_REMOVELIST:print xhxs = HtmlXPathSelector(response)#INFIBEAM_XPATH1 = '//ul[@class="srch_result portrait"]/li'INFIBEAM_XPATH1 = get_code_word("INFIBEAM_XPATH1")phone_info = hxs.select(INFIBEAM_XPATH1)#INFIBEAM_XPATH2 = './/p/span[@class="title"]/text()'INFIBEAM_XPATH2 = get_code_word("INFIBEAM_XPATH2")#INFIBEAM_XPATH3 = './/p/span[@class="price"]/text()'INFIBEAM_XPATH3 = get_code_word("INFIBEAM_XPATH3")items = []if not phone_info:ct = int(da.get_extra_vars('infibeam_count'))if ct>14:fails = int(da.get_extra_vars('infibeam_fails'))fails = fails+1if fails > 0:da.set_extra_vars('infibeam_flag','FALSE','')da.set_extra_vars('infibeam_fails',str(fails),'')else:for i in phone_info:item = {}item['name'] = i.select(INFIBEAM_XPATH2)[0].extract()item['price'] = i.select(INFIBEAM_XPATH3)[0].extract()items.append(item)for i in items:amnt = i['price']if amnt != '':for r in INFIBEAM_REMOVELIST:while amnt.find(r) != -1:amnt = amnt.replace(r, "")amnt = amnt.strip()pr = int(amnt) + int(INFIBEAM_VATPLUSTAX)da.add_infiphone(i['name'], amnt,pr)SPIDER = infi_spider()