-
Notifications
You must be signed in to change notification settings - Fork 0
/
flipkartmobiles.py
51 lines (41 loc) · 1.25 KB
/
flipkartmobiles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import sys
import urllib2
from bs4 import BeautifulSoup
import time
def findTagValueInSoup(soup, tagName, attrsDict):
element = soup.find(tagName, attrsDict)
if element:
try:
elementValue = element.get_text().decode('utf8').encode("ascii","ignore").strip()
except:
elementValue = element.get_text().encode("ascii","ignore").strip()
else:
elementValue = "NA"
return elementValue
def flipkart(flipkartProductURL):
urlSourceCode = urllib2.urlopen(flipkartProductURL.strip())
soup = BeautifulSoup(urlSourceCode)
products=soup.find_all('div',attrs={"class":"pu-details lastUnit"})
for eachProduct in products:
title=findTagValueInSoup(eachProduct,'div',{"class":"pu-title fk-font-13"})
#print title.strip()
link=eachProduct.find('a')
link="http://www.flipkart.com"+link['href'].strip().split('&')[0]
#print link['href']
price=findTagValueInSoup(eachProduct,'div',{"class":"pu-final"})
product=[link,title,price]
#print product
print >> f_out, '\t'.join(product)
f_out = open("mobilesflipkart.csv",'a')
for i in [88,104]:
try:
url=('http://www.flipkart.com/mobiles/pr?p[]=sort=price_asc&sid=tyy,4io&&start=%d')%(20*i)
flipkart(url)
except:
time.sleep(5)
try:
flipkart(url)
except:
print url
time.sleep(5)
continue