| 233 |
ashish |
1 |
'''
|
|
|
2 |
Created on 09-Jun-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 datastore.DataAccessor import *
|
|
|
18 |
from datastore.DataCodeAccessor import *
|
|
|
19 |
|
|
|
20 |
from html2text.unescaping import *
|
|
|
21 |
import urllib
|
|
|
22 |
|
|
|
23 |
class mobilestore_spider0(BaseSpider):
|
| 260 |
ashish |
24 |
"""
|
|
|
25 |
Documentation for class mobilestore_spider0
|
|
|
26 |
This spider collects the information for the individual phones
|
|
|
27 |
and store them in table datastore_datadefinition_themobilestorephones_new
|
|
|
28 |
"""
|
|
|
29 |
|
| 233 |
ashish |
30 |
def __init__(self):
|
| 260 |
ashish |
31 |
"""
|
|
|
32 |
Documentation for constructor
|
|
|
33 |
initialize_table is called to make all the tables known in
|
|
|
34 |
the scope of this class.
|
|
|
35 |
Also start url needs to be feeded to the spider through start_urls.append
|
|
|
36 |
Domainname is name by which this spider is known outside
|
|
|
37 |
So this will be used as an argument for calling this spider.
|
|
|
38 |
As the number of pages to be crawled are not fixed so ct and nt are used to make it dynamic.
|
|
|
39 |
url1 and url2 are used for getting actual start urls.
|
|
|
40 |
"""
|
| 233 |
ashish |
41 |
initialize_table()
|
|
|
42 |
#MOBILESTORE_DOMAINNAME0 = "mobilestore0"
|
|
|
43 |
MOBILESTORE_DOMAINNAME0 = get_code_word("MOBILESTORE_DOMAINNAME0")
|
|
|
44 |
self.domain_name = MOBILESTORE_DOMAINNAME0
|
|
|
45 |
da = DataHelper()
|
|
|
46 |
MOBILESTORE_CT = int(da.get_extra_vars('mobilestore_count'))
|
|
|
47 |
nt = 1
|
|
|
48 |
if MOBILESTORE_CT > 800:
|
|
|
49 |
nt = MOBILESTORE_CT-50
|
|
|
50 |
while nt < MOBILESTORE_CT:
|
|
|
51 |
#MOBILESTORE_URL1 = "http://www.themobilestore.in/mobilestore/faces/tiles/product.jsp?productID=" + str(nt)
|
|
|
52 |
MOBILESTORE_URL1 = get_code_word("MOBILESTORE_URL1") + str(nt)
|
|
|
53 |
#MOBILESTORE_URL2 = MOBILESTORE_URL1 + "&catalogueID=3"
|
|
|
54 |
MOBILESTORE_URL2 = MOBILESTORE_URL1 + get_code_word("MOBILESTORE_URL2")
|
|
|
55 |
self.start_urls.append(MOBILESTORE_URL2)
|
|
|
56 |
nt = nt+1
|
|
|
57 |
|
|
|
58 |
def start_requests(self):
|
| 260 |
ashish |
59 |
"""
|
|
|
60 |
Documentation for method start_requests
|
|
|
61 |
To set various properties of the request to be made
|
|
|
62 |
like referer, headers and all.
|
|
|
63 |
Also suppliers entry need to be done in the table
|
|
|
64 |
datastore_datadefinition_suppliers.
|
|
|
65 |
@return a list of well formed requests which will be
|
|
|
66 |
crawled by spider and spider will return the response
|
|
|
67 |
"""
|
|
|
68 |
#adding entry for the supplier i.e its name and site
|
| 233 |
ashish |
69 |
#MOBILESTORE_HOMEPAGE = "www.themobilestore.in"
|
|
|
70 |
MOBILESTORE_HOMEPAGE = get_code_word("MOBILESTORE_HOMEPAGE")
|
|
|
71 |
da = DataHelper()
|
|
|
72 |
da.add_supplier(self.domain_name, MOBILESTORE_HOMEPAGE)
|
|
|
73 |
listreq = []
|
|
|
74 |
#for each request a referer has to be set
|
|
|
75 |
#MOBILESTORE_REFERER = "www.google.com/search"
|
|
|
76 |
MOBILESTORE_REFERER = get_code_word("MOBILESTORE_REFERER")
|
|
|
77 |
for url1 in self.start_urls:
|
|
|
78 |
request = Request(url = str(url1), callback=self.parse)
|
|
|
79 |
request.headers.setdefault("Referer", MOBILESTORE_REFERER)
|
|
|
80 |
listreq.append(request)
|
|
|
81 |
return listreq
|
|
|
82 |
|
|
|
83 |
def parse(self, response):
|
| 260 |
ashish |
84 |
"""
|
|
|
85 |
Documentation for method parse
|
|
|
86 |
@param response of individual requests
|
|
|
87 |
Using Xpaths needed information is extracted out of the response
|
|
|
88 |
and added to the database
|
|
|
89 |
Xpath3 = Give us name for individual phone
|
|
|
90 |
Xpath4 = Give us price for individual phone
|
|
|
91 |
Xpath5 = Give us name for individual phone, if its not gettable from xpath3
|
|
|
92 |
Xpath6 = Give us name for individual phone, if its not gettable from xpath3 and xpath5
|
|
|
93 |
Xpath7 = to check that the phone can be bought or not
|
|
|
94 |
Xpath8 = to check that the item is mobile phone
|
|
|
95 |
"""
|
| 233 |
ashish |
96 |
da = DataHelper()
|
|
|
97 |
url1 = response.url
|
|
|
98 |
ps1 = url1.find('=')
|
|
|
99 |
ps2 = url1.find('&')
|
|
|
100 |
str1 = url1[ps1+1:ps2]
|
|
|
101 |
#da = DataHelper()
|
|
|
102 |
hxs = HtmlXPathSelector(response)
|
|
|
103 |
#MOBILESTORE_XPATH3 = '//span[@id="productLayoutForm:categoryNavigation:navigationList_2:navigationList3"]/text()'
|
|
|
104 |
MOBILESTORE_XPATH3 = get_code_word("MOBILESTORE_XPATH3")
|
|
|
105 |
#MOBILESTORE_XPATH4 = '//div[@id="priceComp"]//tr[2]/td[3]/span/text()'
|
|
|
106 |
MOBILESTORE_XPATH4 = get_code_word("MOBILESTORE_XPATH4")
|
|
|
107 |
#MOBILESTORE_XPATH5 = '//span[@id="productLayoutForm:categoryNavigation:navigationList_1:navigationList3"]/text()'
|
|
|
108 |
MOBILESTORE_XPATH5 = get_code_word("MOBILESTORE_XPATH5")
|
|
|
109 |
#MOBILESTORE_XPATH6 = '//span[@id="productLayoutForm:categoryNavigation:navigationList_0:navigationList3"]/text()'
|
|
|
110 |
MOBILESTORE_XPATH6 = get_code_word("MOBILESTORE_XPATH6")
|
|
|
111 |
#MOBILESTORE_XPATH7 = '//div[@id="priceComp"]/b/text()'
|
|
|
112 |
MOBILESTORE_XPATH7 = get_code_word("MOBILESTORE_XPATH7")
|
|
|
113 |
#MOBILESTORE_XPATH8 = '//span[@id="productLayoutForm:categoryNavigation:navigationList_0:navigationList1"]/text()'
|
|
|
114 |
MOBILESTORE_XPATH8 = get_code_word("MOBILESTORE_XPATH8")
|
|
|
115 |
try:
|
|
|
116 |
catg = hxs.select(MOBILESTORE_XPATH8)
|
|
|
117 |
catg = catg[0].extract()
|
|
|
118 |
catg = catg.strip()
|
|
|
119 |
catg = unescape(catg)
|
|
|
120 |
print catg
|
|
|
121 |
if catg == "Mobile Phones>":
|
|
|
122 |
try:
|
|
|
123 |
str2 = hxs.select(MOBILESTORE_XPATH7)
|
|
|
124 |
str2 = str2[0].extract()
|
|
|
125 |
str2 = "can buy"
|
|
|
126 |
except:
|
|
|
127 |
str2 = "can not buy"
|
|
|
128 |
try:
|
|
|
129 |
name = hxs.select(MOBILESTORE_XPATH3)
|
|
|
130 |
name = name[0].extract()
|
|
|
131 |
name = name.strip()
|
|
|
132 |
price = hxs.select(MOBILESTORE_XPATH4)
|
|
|
133 |
price = price[0].extract()
|
|
|
134 |
price = price.strip()
|
|
|
135 |
price = int(price)
|
| 260 |
ashish |
136 |
da.add_new_mobstorephone_new(name, price, price, str2)
|
| 233 |
ashish |
137 |
except:
|
|
|
138 |
try:
|
|
|
139 |
name = hxs.select(MOBILESTORE_XPATH5)
|
|
|
140 |
name = name[0].extract()
|
|
|
141 |
name = name.strip()
|
|
|
142 |
price = hxs.select(MOBILESTORE_XPATH4)
|
|
|
143 |
price = price[0].extract()
|
|
|
144 |
price = price.strip()
|
|
|
145 |
price = int(price)
|
| 260 |
ashish |
146 |
da.add_new_mobstorephone_new(name, price, price, str2)
|
| 233 |
ashish |
147 |
except:
|
|
|
148 |
try:
|
|
|
149 |
name = hxs.select(MOBILESTORE_XPATH6)
|
|
|
150 |
name = name[0].extract()
|
|
|
151 |
name = name.strip()
|
|
|
152 |
price = hxs.select(MOBILESTORE_XPATH4)
|
|
|
153 |
price = price[0].extract()
|
|
|
154 |
price = price.strip()
|
|
|
155 |
price = int(price)
|
| 260 |
ashish |
156 |
da.add_new_mobstorephone_new(name, price, price, str2)
|
| 233 |
ashish |
157 |
except:
|
| 260 |
ashish |
158 |
pass
|
| 233 |
ashish |
159 |
except:
|
|
|
160 |
ct = int(da.get_extra_vars('mobilestore_count'))
|
|
|
161 |
if ct>800:
|
|
|
162 |
fails = int(da.get_extra_vars('mobilestore_fails'))
|
|
|
163 |
fails = fails+1
|
|
|
164 |
da.set_extra_vars('mobilestore_fails',str(fails),'')
|
|
|
165 |
if fails > 40:
|
|
|
166 |
da.set_extra_vars('mobilestore_flag','FALSE','')
|
| 260 |
ashish |
167 |
|
| 233 |
ashish |
168 |
SPIDER = mobilestore_spider0()
|