| 149 |
ashish |
1 |
'''
|
|
|
2 |
Created on 13-May-2010
|
|
|
3 |
|
|
|
4 |
@author: gaurav
|
|
|
5 |
'''
|
|
|
6 |
|
|
|
7 |
from scrapy.spider import BaseSpider
|
|
|
8 |
from scrapy.selector import HtmlXPathSelector
|
|
|
9 |
from scrapy.http import Request
|
|
|
10 |
|
|
|
11 |
from demo.items import DemoItem
|
|
|
12 |
from scrapy.contrib.spidermiddleware import referer
|
|
|
13 |
from scrapy.http.headers import Headers
|
|
|
14 |
from scrapy.http.request.form import FormRequest
|
|
|
15 |
from scrapy.log import msg
|
|
|
16 |
from scrapy.http.response import Response
|
|
|
17 |
|
| 228 |
ashish |
18 |
from datastore.DataAccessor import *
|
|
|
19 |
from datastore.DataCodeAccessor import *
|
|
|
20 |
from html2text.unescaping import *
|
|
|
21 |
from elixir import *
|
| 149 |
ashish |
22 |
|
|
|
23 |
class infi_spider(BaseSpider):
|
|
|
24 |
|
| 181 |
ashish |
25 |
def __init__(self):
|
| 228 |
ashish |
26 |
initialize_table()
|
|
|
27 |
da = DataHelper()
|
|
|
28 |
#da.initxy()
|
|
|
29 |
#INFIBEAM_DOMAINNAME = "infibeam"
|
|
|
30 |
INFIBEAM_DOMAINNAME = get_code_word("INFIBEAM_DOMAINNAME")
|
|
|
31 |
print INFIBEAM_DOMAINNAME
|
|
|
32 |
self.domain_name = INFIBEAM_DOMAINNAME
|
|
|
33 |
#INFIBEAM_CT = 15
|
|
|
34 |
#INFIBEAM_CT = int(get_code_word("INFIBEAM_CT"))
|
|
|
35 |
#ct = 15
|
|
|
36 |
ct = int(da.get_extra_vars('infibeam_count'))
|
|
|
37 |
#INFIBEAM_NO = 1
|
| 149 |
ashish |
38 |
no = 1
|
| 228 |
ashish |
39 |
if ct>14:
|
|
|
40 |
no = ct
|
|
|
41 |
#INFIBEAM_URL = "http://www.infibeam.com/Mobiles/search?page="
|
|
|
42 |
INFIBEAM_URL = get_code_word("INFIBEAM_URL")
|
| 149 |
ashish |
43 |
while(no<=ct):
|
| 228 |
ashish |
44 |
url1 = INFIBEAM_URL + str(no)
|
|
|
45 |
self.start_urls.append(url1)
|
| 149 |
ashish |
46 |
no=no+1
|
| 228 |
ashish |
47 |
|
| 149 |
ashish |
48 |
def start_requests(self):
|
| 228 |
ashish |
49 |
#adding entry for the supplier i.e its name and site
|
|
|
50 |
#INFIBEAM_HOMEPAGE = "www.infibeam.com"
|
|
|
51 |
INFIBEAM_HOMEPAGE = get_code_word("INFIBEAM_HOMEPAGE")
|
| 181 |
ashish |
52 |
da = DataHelper()
|
| 228 |
ashish |
53 |
da.add_supplier(self.domain_name, INFIBEAM_HOMEPAGE)
|
| 149 |
ashish |
54 |
listreq = []
|
| 228 |
ashish |
55 |
|
|
|
56 |
#for each request a referer has to be set
|
|
|
57 |
#INFIBEAM_REFERER = "www.google.com/search"
|
|
|
58 |
INFIBEAM_REFERER = get_code_word("INFIBEAM_REFERER")
|
| 149 |
ashish |
59 |
for url1 in self.start_urls:
|
| 228 |
ashish |
60 |
request = Request(url = str(url1), callback=self.parse, dont_filter=True)
|
|
|
61 |
request.headers.setdefault("Referer", INFIBEAM_REFERER)
|
|
|
62 |
listreq.append(request)
|
| 149 |
ashish |
63 |
return listreq
|
| 228 |
ashish |
64 |
|
| 149 |
ashish |
65 |
def parse(self, response):
|
| 228 |
ashish |
66 |
da = DataHelper()
|
|
|
67 |
msg(response.url)
|
|
|
68 |
#INFIBEAM_VATPLUSTAX = 0
|
|
|
69 |
INFIBEAM_VATPLUSTAX = int(get_code_word("INFIBEAM_VATPLUSTAX"))
|
|
|
70 |
#list elements are separated by ';'
|
|
|
71 |
#INFIBEAM_REMOVELIST = ["Rs.",",","-","/","Rs"]
|
|
|
72 |
INFIBEAM_REMOVELIST = get_code_word("INFIBEAM_REMOVELIST")
|
|
|
73 |
INFIBEAM_REMOVELIST = INFIBEAM_REMOVELIST.split(';')
|
| 149 |
ashish |
74 |
hxs = HtmlXPathSelector(response)
|
| 228 |
ashish |
75 |
#INFIBEAM_XPATH1 = '//ul[@class="srch_result portrait"]/li'
|
|
|
76 |
INFIBEAM_XPATH1 = get_code_word("INFIBEAM_XPATH1")
|
|
|
77 |
phone_info = hxs.select(INFIBEAM_XPATH1)
|
|
|
78 |
#INFIBEAM_XPATH2 = './/p/span[@class="title"]/text()'
|
|
|
79 |
INFIBEAM_XPATH2 = get_code_word("INFIBEAM_XPATH2")
|
|
|
80 |
#INFIBEAM_XPATH3 = './/p/span[@class="price"]/text()'
|
|
|
81 |
INFIBEAM_XPATH3 = get_code_word("INFIBEAM_XPATH3")
|
| 149 |
ashish |
82 |
items = []
|
|
|
83 |
msg(response.url)
|
| 228 |
ashish |
84 |
|
|
|
85 |
if not phone_info:
|
|
|
86 |
ct = int(da.get_extra_vars('infibeam_count'))
|
|
|
87 |
if ct>14:
|
|
|
88 |
fails = int(da.get_extra_vars('infibeam_fails'))
|
|
|
89 |
fails = fails+1
|
|
|
90 |
if fails > 0:
|
|
|
91 |
da.set_extra_vars('infibeam_flag','FALSE','')
|
|
|
92 |
da.set_extra_vars('infibeam_fails',str(fails),'')
|
|
|
93 |
else:
|
|
|
94 |
for i in phone_info:
|
|
|
95 |
item = {}
|
|
|
96 |
item['name'] = i.select(INFIBEAM_XPATH2)[0].extract()
|
|
|
97 |
item['price'] = i.select(INFIBEAM_XPATH3)[0].extract()
|
|
|
98 |
items.append(item)
|
| 149 |
ashish |
99 |
|
| 228 |
ashish |
100 |
for i in items:
|
|
|
101 |
amnt = i['price']
|
|
|
102 |
if amnt != '':
|
|
|
103 |
for r in INFIBEAM_REMOVELIST:
|
|
|
104 |
while amnt.find(r) != -1:
|
|
|
105 |
amnt = amnt.replace(r, "")
|
|
|
106 |
amnt = amnt.strip()
|
|
|
107 |
pr = int(amnt) + INFIBEAM_VATPLUSTAX
|
|
|
108 |
da.add_infiphone(i['name'], amnt,pr)
|
|
|
109 |
|
|
|
110 |
|
| 149 |
ashish |
111 |
SPIDER = infi_spider()
|