Subversion Repositories SmartDukaan

Rev

Rev 14543 | Rev 15265 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 14543 Rev 14746
Line 1... Line -...
1
import urllib2
-
 
2
from BeautifulSoup import BeautifulSoup
1
from BeautifulSoup import BeautifulSoup
3
import re
2
from dtr.utils.utils import fetchResponseUsingProxy
4
from sys import exit
3
from sys import exit
5
import json
4
import json
-
 
5
import re
-
 
6
 
6
 
7
 
7
headers = { 
8
headers = { 
8
            'User-agent':'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36',
9
            'User-agent':'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36',
9
            'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',      
10
            'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',      
10
            'Accept-Language' : 'en-US,en;q=0.8',                     
11
            'Accept-Language' : 'en-US,en;q=0.8',                     
11
            'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
12
            'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
12
            'Cookie':'T=TI141257426738726661427143281839817329423126740566618323641725716448; __sonar=7237334677420142002; __gads=ID=c8b82101a0e4f451:T=1412574724:S=ALNI_MbPMbEOZj2nAGjM54z8ZHFMqwTOTQ; FK-CMP-DATA=; SN=2.VI11FB3FB6ED9D4693A796AB8C965B3417.SI802C325AC43444858830E870C4FD3324.VS141257426735693951472.1412576209; VID=2.VI11FB3FB6ED9D4693A796AB8C965B3417.1412576209.VS141257426735693951472; NSID=2.SI802C325AC43444858830E870C4FD3324.1412576209.VI11FB3FB6ED9D4693A796AB8C965B3417; __utma=19769839.709301254.1412574234.1412574234.1412574234.1; __utmb=19769839.23.10.1412574234; __utmc=19769839; __utmz=19769839.1412574234.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); s_cc=true; gpv_pn=SellerListing%3AMobile%3AMicromax%20Canvas%20Fire%20A093; gpv_pn_t=no%20value; s_sq=%5B%5BB%5D%5D; pincode=110011; s_ppv=36',
13
            'Cookie':'T=TI141257426738726661427143281839817329423126740566618323641725716448; __sonar=7237334677420142002; __gads=ID=c8b82101a0e4f451:T=1412574724:S=ALNI_MbPMbEOZj2nAGjM54z8ZHFMqwTOTQ; FK-CMP-DATA=; SN=2.VI11FB3FB6ED9D4693A796AB8C965B3417.SI802C325AC43444858830E870C4FD3324.VS141257426735693951472.1412576209; VID=2.VI11FB3FB6ED9D4693A796AB8C965B3417.1412576209.VS141257426735693951472; NSID=2.SI802C325AC43444858830E870C4FD3324.1412576209.VI11FB3FB6ED9D4693A796AB8C965B3417; __utma=19769839.709301254.1412574234.1412574234.1412574234.1; __utmb=19769839.23.10.1412574234; __utmc=19769839; __utmz=19769839.1412574234.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); s_cc=true; gpv_pn=SellerListing%3AMobile%3AMicromax%20Canvas%20Fire%20A093; gpv_pn_t=no%20value; s_sq=%5B%5BB%5D%5D; pincode=110011; s_ppv=36',
13
            'Connection':'keep-alive'
14
            'Connection':'keep-alive',
-
 
15
            'Accept-Encoding' : 'gzip,deflate,sdch'
14
        }
16
        }
15
 
17
 
16
class FlipkartProductPageScraper:
18
class FlipkartProductPageScraper:
17
    def __init__(self):
19
    def __init__(self):
18
        self.count_trials = 0
20
        self.count_trials = 0
Line 23... Line 25...
23
        redirect_url = ""
25
        redirect_url = ""
24
        try:
26
        try:
25
            
27
            
26
            """quick fix,need to add it conf""" 
28
            """quick fix,need to add it conf""" 
27
            
29
            
28
            proxy = urllib2.ProxyHandler({'http': 'http://192.161.163.60:8800'})
-
 
29
            opener = urllib2.build_opener(proxy)
-
 
30
            urllib2.install_opener(opener)
-
 
31
            response = urllib2.urlopen(url)
-
 
32
            response_data = response.read()
30
            response_data = fetchResponseUsingProxy(url, headers)                
33
            response.close()
-
 
34
            
-
 
35
#            req = urllib2.Request(url,headers=headers)
-
 
36
#            response = urllib2.urlopen(req)
-
 
37
            #response_data = response.read()
-
 
38
            #response.close()
-
 
39
            print "Fetched response from flipkart for %s" %(url)
31
            print "Fetched response from flipkart for %s" %(url)
40
            #redirect_url = response.url
32
            #redirect_url = response.url
41
 
33
 
42
        except Exception as e:
34
        except Exception as e:
-
 
35
            import traceback
-
 
36
            traceback.print_exc()
43
            print 'ERROR: ', e
37
            print 'ERROR: ', e
44
            print 'Retrying'
38
            print 'Retrying'
45
            self.count_trials += 1
39
            self.count_trials += 1
46
 
40
 
47
            if self.count_trials < 3:
41
            if self.count_trials < 3: