import xlrd import logging from parser import PriceParser class Parser

 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
import xlrd
import logging
from parser import PriceParser
class Parser(PriceParser):
compressed_url = 'http://www.btg.ru/common/price/Price_BTG.zip'
compressed_format = 'zip'
compressed_price_fname = 'Price_BTG.xls'
def parse(self, data):
book = xlrd.open_workbook(file_contents=data, encoding_override='cp1251')
sh = book.sheet_by_index(0)
goods = []
for row_counter in xrange(sh.nrows):
row = sh.row(row_counter)
#print ''.join(['%d:%s' % (cnt, x.value) for cnt, x in enumerate(row)]).encode('utf-8')
if len(row) > 2 and row[2].ctype == xlrd.XL_CELL_NUMBER and row[0].value and row[2].value:
goods.append({'name': row[0].value, 'price': row[2].value})
if len(row) > 7 and row[7].ctype == xlrd.XL_CELL_NUMBER and row[5].value and row[7].value:
goods.append({'name': row[5].value, 'price': row[7].value})
return goods
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
parser = Parser()
goods = parser.parse(parser.get())
for item in goods:
print item['name'], item['price']