Subversion Repositories SmartDukaan

Rev

Rev 183 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
183 ashish 1
'''
2
Created on 20-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
 
18
from datastore import DataAccessor
19
from datastore.DataAccessor import DataHelper
231 ashish 20
from html2text.unescaping import *
183 ashish 21
 
22
class mobilestore_spider(BaseSpider):
23
 
24
    def __init__(self): 
231 ashish 25
       MOBILESTORE_DOMAINNAME = "mobilestore"   
26
       self.domain_name = MOBILESTORE_DOMAINNAME 
27
       MOBILESTORE_URL = "http://www.themobilestore.in/sitemap.xml"
28
       self.start_urls.append(MOBILESTORE_URL)
183 ashish 29
 
30
    def start_requests(self):
231 ashish 31
        #adding entry for the supplier i.e its name and site
32
        MOBILESTORE_HOMEPAGE = "www.themobilestore.in"
183 ashish 33
        da = DataHelper()
231 ashish 34
        da.add_supplier(self.domain_name, MOBILESTORE_HOMEPAGE)
183 ashish 35
        listreq = []
231 ashish 36
 
37
        #for each request a referer has to be set
38
        MOBILESTORE_REFERER = "www.google.com/search"
183 ashish 39
        for url1 in self.start_urls:
40
            request = Request(url = str(url1), callback=self.parse)
231 ashish 41
            request.headers.setdefault("Referer", MOBILESTORE_REFERER)
183 ashish 42
            listreq.append(request)
43
        return listreq
231 ashish 44
 
183 ashish 45
    def parse(self, response):
46
        da = DataHelper()
47
        hxs = HtmlXPathSelector(response)
231 ashish 48
        MOBILESTORE_XPATH1 = '//url/loc/text()'
49
        phone_urls = hxs.select(MOBILESTORE_XPATH1)
50
         #elements in chk_list are specific to this site for determining valid sites
51
        MOBILESTORE_CHKLIST1 = ["Mobile_Phones"]
183 ashish 52
        for i in phone_urls:
53
            site = i.extract()
231 ashish 54
            site = unescape(site)
183 ashish 55
            pos1 = pos2 = pos3 = 0
56
            temp =""
57
            pos1 = site.rfind('/')
58
            if pos1 != -1:
59
                pos2 = site.rfind('/',0,pos1-1)
60
            if pos2 != -1:    
61
                pos3 = site.rfind('/',0,pos2-1)
62
            if pos3 > 0:
63
                temp = site[pos3+1:pos2]
231 ashish 64
            # adding valid urls to the DB from the site-map    
65
            for m in MOBILESTORE_CHKLIST1:
66
                if temp == m:
67
                    da.add_mobstoreurl(site)           
183 ashish 68
SPIDER = mobilestore_spider()