Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
'''Created on 09-Jun-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 *import urllibclass mobilestore_spider0(BaseSpider):def __init__(self):initialize_table()#MOBILESTORE_DOMAINNAME0 = "mobilestore0"MOBILESTORE_DOMAINNAME0 = get_code_word("MOBILESTORE_DOMAINNAME0")self.domain_name = MOBILESTORE_DOMAINNAME0da = DataHelper()MOBILESTORE_CT = int(da.get_extra_vars('mobilestore_count'))nt = 1if MOBILESTORE_CT > 800:nt = MOBILESTORE_CT-50while nt < MOBILESTORE_CT:#MOBILESTORE_URL2 = "http://www.themobilestore.in/mobilestore/faces/tiles/product.jsp?productID=600&catalogueID=3"#MOBILESTORE_URL1 = "http://www.themobilestore.in/mobilestore/faces/tiles/product.jsp?productID=" + str(nt)MOBILESTORE_URL1 = get_code_word("MOBILESTORE_URL1") + str(nt)#MOBILESTORE_URL2 = MOBILESTORE_URL1 + "&catalogueID=3"MOBILESTORE_URL2 = MOBILESTORE_URL1 + get_code_word("MOBILESTORE_URL2")self.start_urls.append(MOBILESTORE_URL2)nt = nt+1def start_requests(self):#adding entry for the supplier i.e its name and site#MOBILESTORE_HOMEPAGE = "www.themobilestore.in"MOBILESTORE_HOMEPAGE = get_code_word("MOBILESTORE_HOMEPAGE")da = DataHelper()da.add_supplier(self.domain_name, MOBILESTORE_HOMEPAGE)listreq = []#for each request a referer has to be set#MOBILESTORE_REFERER = "www.google.com/search"MOBILESTORE_REFERER = get_code_word("MOBILESTORE_REFERER")for url1 in self.start_urls:request = Request(url = str(url1), callback=self.parse)request.headers.setdefault("Referer", MOBILESTORE_REFERER)listreq.append(request)return listreqdef parse(self, response):da = DataHelper()#file_to_write1 = "/tmp/phones.csv"#data_file1 = open(file_to_write1,"a")url1 = response.urlps1 = url1.find('=')ps2 = url1.find('&')str1 = url1[ps1+1:ps2]#da = DataHelper()hxs = HtmlXPathSelector(response)#MOBILESTORE_XPATH3 = '//span[@id="productLayoutForm:categoryNavigation:navigationList_2:navigationList3"]/text()'MOBILESTORE_XPATH3 = get_code_word("MOBILESTORE_XPATH3")#MOBILESTORE_XPATH4 = '//div[@id="priceComp"]//tr[2]/td[3]/span/text()'MOBILESTORE_XPATH4 = get_code_word("MOBILESTORE_XPATH4")#MOBILESTORE_XPATH5 = '//span[@id="productLayoutForm:categoryNavigation:navigationList_1:navigationList3"]/text()'MOBILESTORE_XPATH5 = get_code_word("MOBILESTORE_XPATH5")#MOBILESTORE_XPATH6 = '//span[@id="productLayoutForm:categoryNavigation:navigationList_0:navigationList3"]/text()'MOBILESTORE_XPATH6 = get_code_word("MOBILESTORE_XPATH6")#MOBILESTORE_XPATH7 = '//div[@id="priceComp"]/b/text()'MOBILESTORE_XPATH7 = get_code_word("MOBILESTORE_XPATH7")#MOBILESTORE_XPATH8 = '//span[@id="productLayoutForm:categoryNavigation:navigationList_0:navigationList1"]/text()'MOBILESTORE_XPATH8 = get_code_word("MOBILESTORE_XPATH8")try:catg = hxs.select(MOBILESTORE_XPATH8)catg = catg[0].extract()catg = catg.strip()catg = unescape(catg)print catgif catg == "Mobile Phones>":try:str2 = hxs.select(MOBILESTORE_XPATH7)str2 = str2[0].extract()str2 = "can buy"except:str2 = "can not buy"try:name = hxs.select(MOBILESTORE_XPATH3)name = name[0].extract()name = name.strip()price = hxs.select(MOBILESTORE_XPATH4)price = price[0].extract()price = price.strip()price = int(price)da.add_new_mobstorephone_new(name, price, price, str2)#csv_data1 = "%s, %s, %s, %s" %(name, price, str1, str2)#data_file1.write(csv_data1)#data_file1.write("\n")print nameprint priceexcept:try:name = hxs.select(MOBILESTORE_XPATH5)name = name[0].extract()name = name.strip()price = hxs.select(MOBILESTORE_XPATH4)price = price[0].extract()price = price.strip()price = int(price)da.add_new_mobstorephone_new(name, price, price, str2)#csv_data1 = "%s, %s, %s, %s" %(name, price, str1, str2)#data_file1.write(csv_data1)#data_file1.write("\n")print nameprint priceexcept:try:name = hxs.select(MOBILESTORE_XPATH6)name = name[0].extract()name = name.strip()price = hxs.select(MOBILESTORE_XPATH4)price = price[0].extract()price = price.strip()price = int(price)da.add_new_mobstorephone_new(name, price, price, str2)#csv_data1 = "%s, %s, %s, %s" %(name, price, str1, str2)#data_file1.write(csv_data1)#data_file1.write("\n")print nameprint priceexcept:#csv_data2 = "%s" %(str1)#data_file2.write(csv_data2)#data_file2.write("\n")print str1except:ct = int(da.get_extra_vars('mobilestore_count'))if ct>800:fails = int(da.get_extra_vars('mobilestore_fails'))fails = fails+1da.set_extra_vars('mobilestore_fails',str(fails),'')if fails > 40:da.set_extra_vars('mobilestore_flag','FALSE','')print " something else"#data_file1.close()#data_file2.close()SPIDER = mobilestore_spider0()