| Line 1... |
Line -... |
| 1 |
import urllib2
|
- |
|
| 2 |
from BeautifulSoup import BeautifulSoup
|
1 |
from BeautifulSoup import BeautifulSoup
|
| 3 |
import re
|
2 |
import re
|
| 4 |
from sys import exit
|
3 |
from sys import exit
|
| - |
|
4 |
from dtr.utils.utils import fetchResponseUsingProxy
|
| 5 |
|
5 |
|
| 6 |
headers = {
|
6 |
headers = {
|
| 7 |
'User-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
|
7 |
'User-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
|
| 8 |
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
8 |
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
| 9 |
'Accept-Language' : 'en-US,en;q=0.8',
|
9 |
'Accept-Language' : 'en-US,en;q=0.8',
|
| 10 |
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
|
10 |
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
|
| 11 |
'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; s_ppv=36',
|
11 |
'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; s_ppv=36',
|
| 12 |
'Host':'www.flipkart.com'
|
12 |
'Host':'www.flipkart.com',
|
| - |
|
13 |
'Accept-Encoding' : 'gzip,deflate,sdch'
|
| 13 |
}
|
14 |
}
|
| 14 |
|
15 |
|
| 15 |
class FlipkartScraper:
|
16 |
class FlipkartScraper:
|
| 16 |
def __init__(self):
|
17 |
def __init__(self):
|
| 17 |
self.count_trials = 0
|
18 |
self.count_trials = 0
|
| 18 |
self.redirectCount = 0
|
19 |
self.redirectCount = 0
|
| 19 |
|
20 |
|
| 20 |
def read(self, url):
|
21 |
def read(self, url):
|
| 21 |
try:
|
22 |
try:
|
| 22 |
proxy = urllib2.ProxyHandler({'http': 'http://192.161.163.60:8800'})
|
- |
|
| 23 |
opener = urllib2.build_opener(proxy)
|
- |
|
| 24 |
urllib2.install_opener(opener)
|
23 |
"""quick fix,need to add it conf"""
|
| 25 |
response = urllib2.urlopen(url)
|
- |
|
| 26 |
response_data = response.read()
|
- |
|
| 27 |
response.close()
|
- |
|
| 28 |
# try:
|
24 |
|
| 29 |
# response = urllib2.urlopen(request)
|
- |
|
| 30 |
# response_data = response.read()
|
25 |
response_data = fetchResponseUsingProxy(url, headers)
|
| 31 |
# response.close()
|
26 |
|
| 32 |
# print "Fetched response from flipkart for %s" %(url)
|
- |
|
| 33 |
|
- |
|
| 34 |
except Exception as e:
|
27 |
except Exception as e:
|
| 35 |
print 'ERROR: ', e
|
28 |
print 'ERROR: ', e
|
| 36 |
print 'Retrying'
|
29 |
print 'Retrying'
|
| 37 |
self.count_trials += 1
|
30 |
self.count_trials += 1
|
| 38 |
|
31 |
|