| 191 |
ashish |
1 |
'''
|
|
|
2 |
Created on 28-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 |
from time import *
|
| 237 |
ashish |
18 |
from datastore.DataCodeAccessor import *
|
|
|
19 |
from datastore.DataAccessor import *
|
| 191 |
ashish |
20 |
|
|
|
21 |
import urllib
|
| 237 |
ashish |
22 |
from html2text.unescaping import *
|
| 191 |
ashish |
23 |
|
|
|
24 |
class naaptol_price2(BaseSpider):
|
|
|
25 |
|
|
|
26 |
def __init__(self):
|
|
|
27 |
|
| 237 |
ashish |
28 |
initialize_table()
|
|
|
29 |
#NAAPTOL_DOMAINNAME2 = "naaptol2"
|
|
|
30 |
NAAPTOL_DOMAINNAME2 = get_code_word("NAAPTOL_DOMAINNAME2")
|
|
|
31 |
self.domain_name = NAAPTOL_DOMAINNAME2
|
|
|
32 |
|
|
|
33 |
# get urls from the database and append them in the list for crawling
|
| 191 |
ashish |
34 |
da = DataHelper()
|
|
|
35 |
for pitem in da.get_allmorenaaptolurls():
|
|
|
36 |
self.start_urls.append(pitem.url.strip())
|
|
|
37 |
|
|
|
38 |
def start_requests(self):
|
| 237 |
ashish |
39 |
#for each request a referer has to be set
|
| 191 |
ashish |
40 |
listreq = []
|
| 237 |
ashish |
41 |
#NAAPTOL_REFERER = "http://www.google.com"
|
|
|
42 |
NAAPTOL_REFERER = get_code_word("NAAPTOL_REFERER")
|
| 191 |
ashish |
43 |
for url1 in self.start_urls:
|
| 237 |
ashish |
44 |
request = Request(url = str(url1), callback=self.parse)
|
|
|
45 |
request.headers.setdefault("Referer", NAAPTOL_REFERER)
|
| 191 |
ashish |
46 |
listreq.append(request)
|
|
|
47 |
return listreq
|
| 237 |
ashish |
48 |
|
| 191 |
ashish |
49 |
|
|
|
50 |
def parse(self, response):
|
| 237 |
ashish |
51 |
da = DataHelper()
|
|
|
52 |
#NAAPTOL_REMOVELIST = ["Rs.",","]
|
|
|
53 |
#list separated by ';'
|
|
|
54 |
NAAPTOL_REMOVELIST = get_code_word("NAAPTOL_REMOVELIST")
|
|
|
55 |
NAAPTOL_REMOVELIST = NAAPTOL_REMOVELIST.split(';')
|
|
|
56 |
#retreiving name from the the url
|
| 191 |
ashish |
57 |
name = str(response.url)
|
| 237 |
ashish |
58 |
name = unescape(name)
|
| 191 |
ashish |
59 |
name_pos = name.rfind("/")
|
|
|
60 |
name = name[name_pos+1:len(name)-5]
|
| 237 |
ashish |
61 |
name_pos = name.find("-")
|
|
|
62 |
name = name[name_pos+1:len(name)]
|
|
|
63 |
|
| 191 |
ashish |
64 |
hxs = HtmlXPathSelector(response)
|
| 237 |
ashish |
65 |
|
|
|
66 |
#price and price2 determine range
|
|
|
67 |
#NAAPTOL_XPATH2 = '//table[@class ="ProductDetails"]//td[@class="Price"]/span/text()'
|
|
|
68 |
NAAPTOL_XPATH2 = get_code_word("NAAPTOL_XPATH2")
|
|
|
69 |
prices = hxs.select(NAAPTOL_XPATH2)
|
| 191 |
ashish |
70 |
try:
|
|
|
71 |
price1 = prices.extract()[0]
|
| 237 |
ashish |
72 |
#price1 = price1.decode("utf-8")
|
| 191 |
ashish |
73 |
price1 = price1.strip()
|
|
|
74 |
except:
|
|
|
75 |
price1 = ""
|
|
|
76 |
|
|
|
77 |
try:
|
|
|
78 |
price2 = prices.extract()[1]
|
| 237 |
ashish |
79 |
#price2 = price2.decode("utf-8")
|
| 191 |
ashish |
80 |
price2 = price2.strip()
|
|
|
81 |
except:
|
|
|
82 |
price2 = ""
|
|
|
83 |
|
|
|
84 |
try:
|
|
|
85 |
if price1 == "" and price2 == "":
|
| 237 |
ashish |
86 |
#NAAPTOL_XPATH3 = '//table[@class ="ProductDetails"]//td[@class="Price"]/span/script/text()'
|
|
|
87 |
NAAPTOL_XPATH3 = get_code_word("NAAPTOL_XPATH3")
|
|
|
88 |
prices = hxs.select(NAAPTOL_XPATH3)
|
| 191 |
ashish |
89 |
price = str(prices.extract()[0])
|
|
|
90 |
pos1 = price.find("'")
|
|
|
91 |
pos2 = price.find("'",pos1+1,len(price))
|
|
|
92 |
price1 = price[pos1+1:pos2] + "(approx)"
|
|
|
93 |
price2 = ""
|
|
|
94 |
except:
|
|
|
95 |
price1 = price2 = ""
|
| 237 |
ashish |
96 |
#removelist is used for converting price to decimal format containing only numbers and '.'
|
| 191 |
ashish |
97 |
|
|
|
98 |
if price1 != '':
|
| 237 |
ashish |
99 |
for r in NAAPTOL_REMOVELIST:
|
|
|
100 |
while price1.find(r) != -1:
|
|
|
101 |
price1 = price1.replace(r, "")
|
| 191 |
ashish |
102 |
price1 = price1.strip()
|
|
|
103 |
if price2 != '':
|
| 237 |
ashish |
104 |
for r in NAAPTOL_REMOVELIST:
|
|
|
105 |
while price2.find(r) != -1:
|
|
|
106 |
price2 = price2.replace(r, "")
|
| 191 |
ashish |
107 |
price2 = price2.strip()
|
|
|
108 |
|
|
|
109 |
if price1 == "Rates Not Available":
|
|
|
110 |
price1 = price2 = ""
|
|
|
111 |
|
| 237 |
ashish |
112 |
#range = price1 to price2
|
| 191 |
ashish |
113 |
range = price1
|
|
|
114 |
if price2 != "":
|
|
|
115 |
range = str(range) + " to "
|
|
|
116 |
range = range + str(price2)
|
| 237 |
ashish |
117 |
da.add_new_naaptolphone(name, range)
|
| 191 |
ashish |
118 |
|
| 237 |
ashish |
119 |
|
| 191 |
ashish |
120 |
OnlineSellers_pricelist = []
|
|
|
121 |
OnlineSellers_namelist = []
|
|
|
122 |
try:
|
| 237 |
ashish |
123 |
#ct1 holds the count of online sellers
|
|
|
124 |
#NAAPTOL_XPATH4 = '//div[@id="OnlineSellers"]//div[@class="ProductResultHead"]//div[@class="headingstyle"]/text()'
|
|
|
125 |
NAAPTOL_XPATH4 = get_code_word("NAAPTOL_XPATH4")
|
|
|
126 |
ct1 = hxs.select(NAAPTOL_XPATH4)
|
| 191 |
ashish |
127 |
ct1 = str(ct1.extract()[0])
|
|
|
128 |
ct1 = ct1.decode("utf-8")
|
|
|
129 |
ct1 = ct1.strip()
|
|
|
130 |
ps1 = ct1.find(" ")
|
|
|
131 |
ct1 = ct1[0:ps1]
|
|
|
132 |
ct1 = int(ct1)
|
|
|
133 |
except:
|
|
|
134 |
ct1 = 0
|
|
|
135 |
ct = ct1
|
|
|
136 |
i = 0
|
| 237 |
ashish |
137 |
#NAAPTOL_XPATH5 = '//div[@id="onSellerContents"]//td[@class="price"]'
|
|
|
138 |
NAAPTOL_XPATH5 = get_code_word("NAAPTOL_XPATH5")
|
|
|
139 |
os_info = hxs.select(NAAPTOL_XPATH5)
|
| 191 |
ashish |
140 |
while ct > 0:
|
|
|
141 |
os = os_info[i].extract()
|
|
|
142 |
ps1 = os.find(">")
|
|
|
143 |
ps2 = os.find("<",ps1)
|
|
|
144 |
os = os[ps1+1:ps2]
|
| 237 |
ashish |
145 |
|
|
|
146 |
if os != '':
|
|
|
147 |
for r in NAAPTOL_REMOVELIST:
|
|
|
148 |
while os.find(r) != -1:
|
|
|
149 |
os = os.replace(r, "")
|
| 191 |
ashish |
150 |
os = urllib.unquote(os)
|
|
|
151 |
try:
|
|
|
152 |
os = int(os)
|
|
|
153 |
except:
|
| 237 |
ashish |
154 |
#stored in format different than previous one
|
| 191 |
ashish |
155 |
os = os_info[i].extract()
|
|
|
156 |
ps1 = os.find(">",ps2)
|
|
|
157 |
ps2 = os.find("<",ps1)
|
|
|
158 |
os = os[ps1+1:ps2]
|
| 237 |
ashish |
159 |
if os != '':
|
|
|
160 |
for r in NAAPTOL_REMOVELIST:
|
|
|
161 |
while os.find(r) != -1:
|
|
|
162 |
os = os.replace(r, "")
|
| 191 |
ashish |
163 |
os = urllib.unquote(os)
|
|
|
164 |
os = int(os)
|
| 237 |
ashish |
165 |
|
| 191 |
ashish |
166 |
OnlineSellers_pricelist.append(os)
|
|
|
167 |
|
| 237 |
ashish |
168 |
#NAAPTOL_XPATH6 = '//div[@id="onSellerContents"]//tr[@class="DottedBorder"]/td/a[@id="storeInfoPop'
|
|
|
169 |
NAAPTOL_XPATH6 = get_code_word("NAAPTOL_XPATH6")
|
|
|
170 |
#NAAPTOL_XPATH7 = '"]/span/text()'
|
|
|
171 |
NAAPTOL_XPATH7 = get_code_word("NAAPTOL_XPATH7")
|
|
|
172 |
NAAPTOL_XPATH6 = NAAPTOL_XPATH6 + str(i)
|
|
|
173 |
NAAPTOL_XPATH6 = NAAPTOL_XPATH6 + NAAPTOL_XPATH7
|
|
|
174 |
path = NAAPTOL_XPATH6
|
| 191 |
ashish |
175 |
osname = hxs.select(path)
|
|
|
176 |
osname = osname.extract()[0]
|
| 237 |
ashish |
177 |
osname = unescape(osname)
|
| 191 |
ashish |
178 |
osname = urllib.unquote(osname)
|
|
|
179 |
OnlineSellers_namelist.append(osname)
|
|
|
180 |
i = i+1
|
|
|
181 |
ct = ct-1
|
|
|
182 |
|
|
|
183 |
l = len(OnlineSellers_pricelist)
|
| 237 |
ashish |
184 |
i = 0
|
| 191 |
ashish |
185 |
nid = da.get_naaptolphone(name,range).id
|
|
|
186 |
while l > 0:
|
|
|
187 |
da.add_new_ntonlinesp(nid, OnlineSellers_namelist[i], OnlineSellers_pricelist[i])
|
|
|
188 |
i = i+1
|
|
|
189 |
l = l-1
|
|
|
190 |
|
|
|
191 |
LocalSellers_pricelist = []
|
|
|
192 |
LocalSellers_namelist = []
|
|
|
193 |
try:
|
| 237 |
ashish |
194 |
#ct1 holds the count of online sellers
|
|
|
195 |
#NAAPTOL_XPATH8 = '//div[@id="LocalStores"]//div[@class="ProductResultHead"]//div[@class="headingstyle"]/text()'
|
|
|
196 |
NAAPTOL_XPATH8 = get_code_word("NAAPTOL_XPATH8")
|
|
|
197 |
ct1 = hxs.select(NAAPTOL_XPATH8)
|
| 191 |
ashish |
198 |
ct1 = str(ct1.extract()[0])
|
|
|
199 |
ct1 = ct1.decode("utf-8")
|
|
|
200 |
ct1 = ct1.strip()
|
|
|
201 |
ps1 = ct1.find(" ")
|
|
|
202 |
ct1 = ct1[0:ps1]
|
|
|
203 |
ct1 = int(ct1)
|
|
|
204 |
except:
|
|
|
205 |
ct1 = 0
|
|
|
206 |
ct = ct1
|
|
|
207 |
i = 0
|
| 237 |
ashish |
208 |
#NAAPTOL_XPATH9 = '//div[@id="offSellerContents"]//td[@class="price"]'
|
|
|
209 |
NAAPTOL_XPATH9 = get_code_word("NAAPTOL_XPATH9")
|
|
|
210 |
#NAAPTOL_XPATH10 = '//div[@id="offSellerContents"]//span[@class="LocalStoreHeading"]/text()'
|
|
|
211 |
NAAPTOL_XPATH10 = get_code_word("NAAPTOL_XPATH10")
|
|
|
212 |
os_info = hxs.select(NAAPTOL_XPATH9)
|
|
|
213 |
os_names = hxs.select(NAAPTOL_XPATH10)
|
|
|
214 |
|
| 191 |
ashish |
215 |
while ct > 0:
|
|
|
216 |
os = os_info[i].extract()
|
|
|
217 |
osname = os_names[i].extract()
|
|
|
218 |
ps1 = os.find(">")
|
|
|
219 |
ps2 = os.find("<",ps1)
|
|
|
220 |
os = os[ps1+1:ps2]
|
| 237 |
ashish |
221 |
if os != '':
|
|
|
222 |
for r in NAAPTOL_REMOVELIST:
|
|
|
223 |
while os.find(r) != -1:
|
|
|
224 |
os = os.replace(r, "")
|
| 191 |
ashish |
225 |
os = urllib.unquote(os)
|
|
|
226 |
osname = urllib.unquote(osname)
|
| 237 |
ashish |
227 |
osname = unescape(osname)
|
| 191 |
ashish |
228 |
try:
|
|
|
229 |
os = int(os)
|
|
|
230 |
except:
|
| 237 |
ashish |
231 |
#stored in format different than previous one
|
| 191 |
ashish |
232 |
os = os_info[i].extract()
|
|
|
233 |
ps1 = os.find(">",ps2)
|
|
|
234 |
ps2 = os.find("<",ps1)
|
|
|
235 |
os = os[ps1+1:ps2]
|
| 237 |
ashish |
236 |
if os != '':
|
|
|
237 |
for r in NAAPTOL_REMOVELIST:
|
|
|
238 |
while os.find(r) != -1:
|
|
|
239 |
os = os.replace(r, "")
|
| 191 |
ashish |
240 |
os = urllib.unquote(os)
|
|
|
241 |
os = int(os)
|
|
|
242 |
LocalSellers_pricelist.append(os)
|
|
|
243 |
LocalSellers_namelist.append(osname)
|
|
|
244 |
i = i+1
|
|
|
245 |
ct = ct-1
|
|
|
246 |
|
|
|
247 |
l = len(LocalSellers_pricelist)
|
|
|
248 |
i = 0
|
|
|
249 |
nid = da.get_naaptolphone(name,range).id
|
|
|
250 |
while l > 0:
|
|
|
251 |
da.add_new_ntofflinesp(nid, LocalSellers_namelist[i], LocalSellers_pricelist[i])
|
|
|
252 |
i = i+1
|
| 237 |
ashish |
253 |
l = l-1
|
|
|
254 |
|
| 191 |
ashish |
255 |
SPIDER = naaptol_price2()
|
|
|
256 |
|