| 154 |
ashish |
1 |
'''
|
|
|
2 |
Created on 14-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
|
|
|
18 |
|
|
|
19 |
|
| 240 |
ashish |
20 |
from datastore.DataCodeAccessor import *
|
|
|
21 |
from datastore.DataAccessor import *
|
|
|
22 |
from html2text.unescaping import *
|
|
|
23 |
|
| 154 |
ashish |
24 |
class univercell_price(BaseSpider):
|
|
|
25 |
|
|
|
26 |
def __init__(self):
|
| 240 |
ashish |
27 |
initialize_table()
|
|
|
28 |
#UNIVERCELL_DOMAINNAME1 = "univercell1"
|
|
|
29 |
UNIVERCELL_DOMAINNAME1 = get_code_word("UNIVERCELL_DOMAINNAME1")
|
|
|
30 |
self.domain_name = UNIVERCELL_DOMAINNAME1
|
|
|
31 |
|
|
|
32 |
# get urls from the database and append them in the list for crawling
|
| 154 |
ashish |
33 |
da = DataHelper()
|
|
|
34 |
for pitem in da.get_all_univervendors():
|
|
|
35 |
self.start_urls.append(pitem.v_site.strip())
|
|
|
36 |
|
|
|
37 |
def start_requests(self):
|
| 240 |
ashish |
38 |
|
|
|
39 |
#for each request a referer has to be set
|
| 154 |
ashish |
40 |
listreq = []
|
| 240 |
ashish |
41 |
#UNIVERCELL_REFERER = "www.google.com/search"
|
|
|
42 |
UNIVERCELL_REFERER = get_code_word("UNIVERCELL_REFERER")
|
| 154 |
ashish |
43 |
for url1 in self.start_urls:
|
| 240 |
ashish |
44 |
request = Request(url = str(url1), callback=self.parse)
|
|
|
45 |
request.headers.setdefault("Referer", UNIVERCELL_REFERER)
|
| 154 |
ashish |
46 |
listreq.append(request)
|
|
|
47 |
return listreq
|
| 240 |
ashish |
48 |
|
| 154 |
ashish |
49 |
def parse(self, response):
|
| 170 |
ashish |
50 |
da = DataHelper()
|
| 240 |
ashish |
51 |
#VATPLUSTAX = 0
|
|
|
52 |
#removelist is used for converting price to decimal format containing only numbers and '.'
|
|
|
53 |
#UNIVERCELL_REMOVELIST = ["Rs",",","-","/"]
|
|
|
54 |
#list separated by ';'
|
|
|
55 |
UNIVERCELL_REMOVELIST = get_code_word("UNIVERCELL_REMOVELIST")
|
|
|
56 |
UNIVERCELL_REMOVELIST = UNIVERCELL_REMOVELIST.split(';')
|
| 154 |
ashish |
57 |
hxs = HtmlXPathSelector(response)
|
| 240 |
ashish |
58 |
#UNIVERCELL_XPATH4 = '//td[@class="gray-border"]'
|
|
|
59 |
UNIVERCELL_XPATH4 = get_code_word("UNIVERCELL_XPATH4")
|
|
|
60 |
sites = hxs.select(UNIVERCELL_XPATH4)
|
| 154 |
ashish |
61 |
items = []
|
|
|
62 |
for site in sites:
|
|
|
63 |
item = {}
|
| 240 |
ashish |
64 |
#UNIVERCELL_XPATH5 = './/tr[2]/td/a/text()'
|
|
|
65 |
UNIVERCELL_XPATH5 = get_code_word("UNIVERCELL_XPATH5")
|
|
|
66 |
item['title'] = site.select(UNIVERCELL_XPATH5)[0].extract()
|
|
|
67 |
#UNIVERCELL_XPATH6 = './/tr[3]/th/label/text()'
|
|
|
68 |
UNIVERCELL_XPATH6 = get_code_word("UNIVERCELL_XPATH6")
|
|
|
69 |
item['price'] =site.select(UNIVERCELL_XPATH6)[0].extract()
|
| 154 |
ashish |
70 |
items.append(item)
|
| 170 |
ashish |
71 |
|
| 154 |
ashish |
72 |
for i in items:
|
| 240 |
ashish |
73 |
str1 = str(i['title']).strip()
|
|
|
74 |
amnt = i['price']
|
|
|
75 |
if amnt != '':
|
|
|
76 |
for r in UNIVERCELL_REMOVELIST:
|
|
|
77 |
while amnt.find(r) != -1:
|
|
|
78 |
amnt = amnt.replace(r, "")
|
| 170 |
ashish |
79 |
amnt = amnt.strip()
|
| 240 |
ashish |
80 |
# 4% additional vat is there on the price
|
|
|
81 |
UNIVERCELL_VATPLUSTAX = 4*int(amnt)/100
|
|
|
82 |
pr = int(amnt) + UNIVERCELL_VATPLUSTAX
|
|
|
83 |
#adding model-name,quotedprice and finalprice
|
|
|
84 |
da.add_new_univerphone(unescape(str1),amnt,pr)
|
|
|
85 |
|
| 154 |
ashish |
86 |
SPIDER = univercell_price()
|
|
|
87 |
|