Rev 236 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
'''Created on 27-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 time import *from datastore import DataAccessorfrom datastore.DataAccessor import DataHelperimport urllibclass naaptol_price(BaseSpider):def __init__(self):self.domain_name = "naaptolphones"#self.start_urls.append("http://www.naaptol.com/price/10415-Fly-Hummer-HT1.html")da = DataHelper()for pitem in da.get_allnaaptolurls():self.start_urls.append(pitem.url.strip())def start_requests(self):listreq = []for url1 in self.start_urls:request = Request(url = url1, callback=self.parse)request.headers.setdefault("Referer", "www.naaptol.com")listreq.append(request)return listreqdef parse(self, response):#msg(response.body)site = response.urlsp1 = site.rfind("/")sp2 = site.rfind("/",0,sp1-1)catg = site[sp2+1:sp1]da = DataHelper()if catg == "price":site = site.replace("price","features")da.add_morenaaptolurl(site)else:f = open('/home/gaurav/Desktop/response.txt', 'w')f.write(response.body)print " url " + response.urlname = str(response.url)name_pos = name.rfind("/")name = name[name_pos+1:len(name)-5]hxs = HtmlXPathSelector(response)#prices = hxs.select('//table[@class ="ProductDetails"]/tbody/tr[6]/td/span/text()')prices = hxs.select('//table[@class ="ProductDetails"]//td[@class="Price"]/span/text()')try:price1 = prices.extract()[0]price1 = price1.decode("utf-8")price1 = price1.strip()except:price1 = ""try:price2 = prices.extract()[1]price2 = price2.decode("utf-8")price2 = price2.strip()except:price2 = ""try:if price1 == "" and price2 == "":prices = hxs.select('//table[@class ="ProductDetails"]//td[@class="Price"]/span/script/text()')price = str(prices.extract()[0])pos1 = price.find("'")pos2 = price.find("'",pos1+1,len(price))price1 = price[pos1+1:pos2] + "(approx)"price2 = ""except:price1 = price2 = ""if price1 != '':price1 = price1.replace("Rs.", "")price1 = price1.replace(",", "")price1 = price1.strip()if price2 != '':price2 = price2.replace("Rs.", "")price2 = price2.replace(",", "")price2 = price2.strip()if price1 == "Rates Not Available":price1 = price2 = ""print nameprint price1print price2print "\n"range = price1if price2 != "":range = str(range) + " to "range = range + str(price2)da.add_new_naaptolphone(name, range)OnlineSellers_pricelist = []OnlineSellers_namelist = []try:ct1 = hxs.select('//div[@id="OnlineSellers"]//div[@class="ProductResultHead"]//div[@class="headingstyle"]/text()')ct1 = str(ct1.extract()[0])ct1 = ct1.decode("utf-8")ct1 = ct1.strip()ps1 = ct1.find(" ")ct1 = ct1[0:ps1]ct1 = int(ct1)except:ct1 = 0ct = ct1i = 0os_info = hxs.select('//div[@id="onSellerContents"]//td[@class="price"]')#print len(os_info)while ct > 0:os = os_info[i].extract()ps1 = os.find(">")ps2 = os.find("<",ps1)os = os[ps1+1:ps2]os = os.replace("Rs.", "")os = os.replace(",", "")os = urllib.unquote(os)try:os = int(os)except:os = os_info[i].extract()ps1 = os.find(">",ps2)ps2 = os.find("<",ps1)os = os[ps1+1:ps2]os = os.replace("Rs.", "")os = os.replace(",", "")os = urllib.unquote(os)os = int(os)print osOnlineSellers_pricelist.append(os)path = '//div[@id="onSellerContents"]//tr[@class="DottedBorder"]/td/a[@id="storeInfoPop'path = path + str(i)path = path + '"]/span/text()'osname = hxs.select(path)#print len(osname)osname = osname.extract()[0]osname = urllib.unquote(osname)OnlineSellers_namelist.append(osname)print osnamei = i+1ct = ct-1l = len(OnlineSellers_pricelist)i = 0nid = da.get_naaptolphone(name,range).idwhile l > 0:da.add_new_ntonlinesp(nid, OnlineSellers_namelist[i], OnlineSellers_pricelist[i])#print OnlineSellers_list[i]i = i+1l = l-1LocalSellers_pricelist = []LocalSellers_namelist = []try:ct1 = hxs.select('//div[@id="LocalStores"]//div[@class="ProductResultHead"]//div[@class="headingstyle"]/text()')ct1 = str(ct1.extract()[0])ct1 = ct1.decode("utf-8")ct1 = ct1.strip()ps1 = ct1.find(" ")ct1 = ct1[0:ps1]ct1 = int(ct1)except:ct1 = 0ct = ct1i = 0os_info = hxs.select('//div[@id="offSellerContents"]//td[@class="price"]')os_names = hxs.select('//div[@id="offSellerContents"]//span[@class="LocalStoreHeading"]/text()')#print len(os_info)while ct > 0:os = os_info[i].extract()osname = os_names[i].extract()#os = os.encode("utf-8")ps1 = os.find(">")ps2 = os.find("<",ps1)os = os[ps1+1:ps2]os = os.replace("Rs.", "")os = os.replace(",", "")os = urllib.unquote(os)osname = urllib.unquote(osname)try:os = int(os)except:os = os_info[i].extract()ps1 = os.find(">",ps2)ps2 = os.find("<",ps1)os = os[ps1+1:ps2]os = os.replace("Rs.", "")os = os.replace(",", "")os = urllib.unquote(os)os = int(os)print osprint osnameLocalSellers_pricelist.append(os)LocalSellers_namelist.append(osname)i = i+1ct = ct-1l = len(LocalSellers_pricelist)i = 0nid = da.get_naaptolphone(name,range).idwhile l > 0:da.add_new_ntofflinesp(nid, LocalSellers_namelist[i], LocalSellers_pricelist[i])i = i+1l = l-1'''l = len(OnlineSellers_list)i = 0while l > 0:#print OnlineSellers_list[i]i = i+1l = l-1'''f.close()#del DataHelper'''site = response.urlvatplustax = 0pos1 = pos2 = 0temp = ""pos1 = site.rfind('/')if pos1 != -1:temp = site[pos1+1:len(site)]#pos2 = site.rfind('/',0,pos1-1)#if pos2 > 0:#temp = site[pos2+1:len(site)]pos3 = temp.find('.')temp1 = temp[pos3:len(temp)]name = temp.replace(temp1,"")hxs = HtmlXPathSelector(response)prices = hxs.select('//div[@id ="priceComp"]//tr[2]/td[3]/span/text()')da = DataHelper()for price in prices:name = str(name).strip()price = price.extract()price = str(price).strip()price = price.replace("Rs", "")price = price.replace("/", "")price = price.replace("-", "")price = price.replace(".", "")shown_pr = int(price)final_pr = shown_pr + vatplustaxda.add_new_mobstorephone(name,shown_pr,final_pr)print nameprint final_prprint "\n"for i in items:str1 = str(i['title']).strip()print str1amnt = i['price'].replace(",","")amnt = amnt.replace("Rs", "")amnt = amnt.replace("/", "")amnt = amnt.replace("-", "")amnt = amnt.strip()vatplustax = 4*int(amnt)/100pr = int(amnt) + vatplustax#print prda.add_new_univerphone(str1,amnt,pr)'''SPIDER = naaptol_price()