forked from SkyRzn/a62prices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prices.py
executable file
·88 lines (63 loc) · 1.68 KB
/
prices.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests, re, json, time, os, sys, data
from lxml import html
reload(sys)
sys.setdefaultencoding('utf8')
def parse_page(url, res):
sys.stdout.write('.')
sys.stdout.flush()
try:
page = html.parse(url)
page = page.getroot()
except:
raise Exception('Parsing error!')
return
products = page.find_class('product-grid')[0].findall('div')
products = filter(lambda x: not x.keys(), products)
for product in products:
name = product.find_class('name')[0].find('a').text
art = product.find_class('art')[0].text
art = int(re.search('[0-9]+', art).group())
price = product.find_class('price-new')
action = bool(price)
if price:
price = price[0]
else:
price = product.find_class('price')[0]
try:
price = float(price.text.strip().replace(' ', ''))
except:
raise Exception('Price error! (%s)' % price.text)
return
presence = bool(product.find_class('quantity yes'))
res[art] = (name, price, presence, action)
links = page.find_class('links')[0]
pageLinks = links.findall('a')
for pl in pageLinks:
if pl.text == '>':
parse_page(pl.values()[0], res)
return
def get_prices(key, id, name):
url = 'http://assorti62.ru/index.php?route=product/category&path=%d&limit=25' % (id)
print name,
sys.stdout.flush()
res = {}
parse_page(url, res)
print 'ok'
date = time.strftime('%y.%m.%d')
try:
os.mkdir('data/%s' % date)
except:
pass
f = open('data/%s/%s.json' % (date, key), 'w')
text = json.dumps(res)
f.write(text)
f.close
return None
def main():
for key, val in data.categories.items():
id, name = val
get_prices(key, id, name)
if __name__ == '__main__':
main()