| 14122 |
kshitij.so |
1 |
from BeautifulSoup import BeautifulSoup
|
| 14746 |
kshitij.so |
2 |
from dtr.utils.utils import fetchResponseUsingProxy
|
| 14122 |
kshitij.so |
3 |
from sys import exit
|
|
|
4 |
import json
|
| 14746 |
kshitij.so |
5 |
import re
|
| 15265 |
kshitij.so |
6 |
import traceback
|
| 14122 |
kshitij.so |
7 |
|
| 14746 |
kshitij.so |
8 |
|
| 14122 |
kshitij.so |
9 |
headers = {
|
|
|
10 |
'User-agent':'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36',
|
|
|
11 |
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
|
12 |
'Accept-Language' : 'en-US,en;q=0.8',
|
|
|
13 |
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
|
|
|
14 |
'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',
|
| 14746 |
kshitij.so |
15 |
'Connection':'keep-alive',
|
|
|
16 |
'Accept-Encoding' : 'gzip,deflate,sdch'
|
| 14122 |
kshitij.so |
17 |
}
|
|
|
18 |
|
|
|
19 |
class FlipkartProductPageScraper:
|
|
|
20 |
def __init__(self):
|
|
|
21 |
self.count_trials = 0
|
|
|
22 |
self.redirectCount = 0
|
|
|
23 |
|
|
|
24 |
def read(self, url):
|
|
|
25 |
response_data = ""
|
|
|
26 |
redirect_url = ""
|
|
|
27 |
try:
|
| 14538 |
kshitij.so |
28 |
|
|
|
29 |
"""quick fix,need to add it conf"""
|
|
|
30 |
|
| 14746 |
kshitij.so |
31 |
response_data = fetchResponseUsingProxy(url, headers)
|
| 14122 |
kshitij.so |
32 |
print "Fetched response from flipkart for %s" %(url)
|
| 14538 |
kshitij.so |
33 |
#redirect_url = response.url
|
| 14122 |
kshitij.so |
34 |
|
|
|
35 |
except Exception as e:
|
| 14746 |
kshitij.so |
36 |
traceback.print_exc()
|
| 14122 |
kshitij.so |
37 |
print 'ERROR: ', e
|
|
|
38 |
print 'Retrying'
|
|
|
39 |
self.count_trials += 1
|
|
|
40 |
|
|
|
41 |
if self.count_trials < 3:
|
|
|
42 |
return self.read(url)
|
|
|
43 |
|
|
|
44 |
self.response_data=response_data
|
|
|
45 |
return self.createData(url,redirect_url)
|
|
|
46 |
|
|
|
47 |
def scrapeRedirectedPage(self,soup,redirect_url):
|
|
|
48 |
print soup
|
|
|
49 |
print redirect_url
|
|
|
50 |
t = soup.find("div" , {"class" : "seller-table fk-user-select-none line"})
|
|
|
51 |
print t
|
|
|
52 |
table_rows = t.findAll("tr" , {"class" : re.compile('t-row.*')})
|
|
|
53 |
print table_rows
|
|
|
54 |
for x in table_rows:
|
|
|
55 |
print x
|
|
|
56 |
|
|
|
57 |
def createData(self,url, redirect_url):
|
|
|
58 |
print "Creating soup from flipkart data for %s" %(url)
|
|
|
59 |
#redirect_url = redirect_url.replace('www.flipkart.com','163.53.77.21')
|
|
|
60 |
print "Redirect url is %s"%(redirect_url)
|
|
|
61 |
page=self.response_data.decode("utf-8")
|
|
|
62 |
self.soup = BeautifulSoup(page,convertEntities=BeautifulSoup.HTML_ENTITIES)
|
|
|
63 |
page = None
|
|
|
64 |
self.response_data = None
|
|
|
65 |
print "Soup created from flipkart data for %s" %(url)
|
| 14333 |
kshitij.so |
66 |
print redirect_url
|
|
|
67 |
return self.scrape(self.soup,url)
|
| 14122 |
kshitij.so |
68 |
|
|
|
69 |
def scrape(self,soup,url):
|
|
|
70 |
try:
|
|
|
71 |
print "data-config"
|
|
|
72 |
if soup.find('div',{'class':'seller-table-wrap section'}) is None:
|
|
|
73 |
raise
|
|
|
74 |
x = json.loads(soup.find('div',{'class':'seller-table-wrap section'})['data-config'])['dataModel']
|
|
|
75 |
lines = sorted(x, key=lambda k: k['priceInfo'].get('sellingPrice', 0), reverse=False)
|
|
|
76 |
sellingPrice = float(lines[0]['priceInfo']['sellingPrice'])
|
|
|
77 |
try:
|
|
|
78 |
offerText = lines[0]['offerInfo']['listingOffers'][0]['description']
|
|
|
79 |
except:
|
|
|
80 |
offerText = ""
|
| 15265 |
kshitij.so |
81 |
buyBoxPrice = float(soup.find('span',{'class':'selling-price omniture-field'})['data-evar48'])
|
|
|
82 |
return {'lowestSp':sellingPrice,'inStock':1,'buyBoxPrice':buyBoxPrice}
|
| 14122 |
kshitij.so |
83 |
except:
|
| 15265 |
kshitij.so |
84 |
"""No able to parse seller wrap section, probably due to only single seller option"""
|
|
|
85 |
buyBoxPrice = float(soup.find('span',{'class':'selling-price omniture-field'})['data-evar48'])
|
|
|
86 |
sellingPrice = buyBoxPrice
|
|
|
87 |
if soup.find('div',{'class':'out-of-stock'}) is not None:
|
|
|
88 |
inStock = 0
|
|
|
89 |
else:
|
|
|
90 |
inStock = 1
|
|
|
91 |
return {'lowestSp':sellingPrice,'inStock':inStock,'buyBoxPrice':buyBoxPrice}
|
| 14122 |
kshitij.so |
92 |
|
|
|
93 |
if __name__ == '__main__':
|
|
|
94 |
scraper = FlipkartProductPageScraper()
|
| 15265 |
kshitij.so |
95 |
print scraper.read('http://www.flipkart.com/samsung-galaxy-star-advance/p/itmeyfc4vwzwhuva?pid=MOBEYFC44EUDZ9TX')
|