-
Notifications
You must be signed in to change notification settings - Fork 0
/
mido.py
175 lines (146 loc) · 5.36 KB
/
mido.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# coding=utf-8
import json
import string
import re
import common as cm
import geosense as gs
__author__ = 'Zephyre'
db = None
def fetch_stores(data):
"""
商店列表
:param data:
"""
html = data['html']
store_list = []
while True:
entry = cm.init_store_entry(data['brand_id'], data['brandname_e'], data['brandname_c'])
m = re.search(ur'<li class="leaf end"><div><u>(.+?)</u>', html)
if m is None:
break
html = html[m.start():]
entry[cm.name_e] = m.group(1)
sub, start, end = cm.extract_closure(html, ur'<li\b', '</li>')
html = html[end:]
# 单个商店的页面
sub = sub[len(m.group(0)):-len('</li>')]
m = re.search(ur'<a href="(http.+?)"', sub)
if m is not None:
entry[cm.url] = m.group(1)
m = re.search(ur'<a href="mailto:(.+?)"', sub)
if m is not None:
entry[cm.email] = m.group(1)
m = re.search(ur'(?:<a\b|</div>)', sub)
if m is not None:
addr = sub[:m.start()]
else:
addr = sub
# 解析地址栏
addr = cm.reformat_addr(addr)
terms = addr.split(',')
new_terms = []
for t in terms:
if re.search(ur'phone', t, re.IGNORECASE) is not None:
entry[cm.tel] = cm.extract_tel(t)
elif re.search(ur'fax', t, re.IGNORECASE) is not None:
entry[cm.fax] = cm.extract_tel(t)
elif data['city_e'] in t.strip().upper():
# 邮编
m = re.search(ur'\d+', t)
if m is not None:
entry[cm.zip_code] = m.group(0)
else:
new_terms.append(t)
entry[cm.addr_e] = ', '.join(new_terms)
if cm.city_e in data:
entry[cm.city_e] = data['city_e']
if cm.city_c in data:
entry[cm.city_c] = data['city_c']
if cm.country_e in data:
entry[cm.country_e] = data['country_e']
if cm.country_c in data:
entry[cm.country_e] = data['country_c']
if 'province_e' in data:
entry[cm.province_e] = data['province_e']
entry[cm.city_e] = cm.extract_city(entry[cm.city_e])[0]
gs.field_sense(entry)
ret = gs.addr_sense(entry[cm.addr_e], entry[cm.country_e])
if ret[1] is not None and entry[cm.province_e] == '':
entry[cm.province_e] = ret[1]
if ret[2] is not None and entry[cm.city_e] == '':
entry[cm.city_e] = ret[2]
gs.field_sense(entry)
print '(%s / %d) Found store: %s, %s (%s, %s, %s)' % (data['brandname_e'], data['brand_id'],
entry[cm.name_e], entry[cm.addr_e], entry[cm.city_e],
entry[cm.country_e], entry[cm.continent_e])
store_list.append(entry)
db.insert_record(entry, 'stores')
return store_list
def fetch_cities(data):
"""
城市列表
:param data:
"""
html = data['html']
store_list = []
while True:
m = re.search(ur'<li class="expanded"><a href=".*?">(.+?)</a><br\s*?/>', html)
if m is None:
break
html = html[m.start():]
sub, start, end = cm.extract_closure(html, ur'<li\b', '</li>')
html = html[end:]
d = data.copy()
d['html'] = sub[len(m.group(0)):-len('</li>')]
terms = m.group(1).strip().upper().split(' ')
if len(terms) > 1 and cm.is_chinese(terms[-1]):
d['city_c'] = terms[-1].strip()
terms = terms[:-1]
d['city_e'] = ' '.join(terms)
if d['country_e'] == 'USA':
m1 = re.search(ur'([A-Z]{2})\s*-\s*(.+)', d['city_e'])
if m1:
d['city_e'] = m1.group(2).strip()
d['province_e'] = m1.group(1).strip()
print 'Processing %s' % d['city_e']
store_list.extend(fetch_stores(d))
return store_list
def fetch_countries(data):
"""
国家列表
:param data:
"""
html = data['html']
store_list = []
while True:
m = re.search(ur'<li class="expanded"><a href=".*?">(.+?)</a><br\s*?/>', html)
if m is None:
break
html = html[m.start():]
sub, start, end = cm.extract_closure(html, ur'<li\b', '</li>')
html = html[end:]
d = data.copy()
d['html'] = sub[len(m.group(0)):-len('</li>')]
d['country_e'] = m.group(1).strip().upper()
print 'Processing %s' % d['country_e']
store_list.extend(fetch_cities(d))
return store_list
def fetch(level=1, data=None, user='root', passwd=''):
# Walk from the root node, where level == 1.
if data is None:
data = {'url': 'http://www.mido.cn/zh/retailer_li/POS',
'brand_id': 10260, 'brandname_e': u'MIDO', 'brandname_c': u'美度'}
global db
db = cm.StoresDb()
db.connect_db(user=user, passwd=passwd)
db.execute(u'DELETE FROM %s WHERE brand_id=%d' % ('stores', data['brand_id']))
url = data['url']
try:
data['html'] = cm.get_data(url)
except Exception:
print 'Error occured: %s' % url
dump_data = {'level': 0, 'time': cm.format_time(), 'data': {'url': url}, 'brand_id': data['brand_id']}
cm.dump(dump_data)
return []
store_list = fetch_countries(data)
db.disconnect_db()