-
Notifications
You must be signed in to change notification settings - Fork 0
/
bse.py
195 lines (163 loc) · 7.52 KB
/
bse.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# -*- coding: utf-8 -*-
import os, re, random
import requests
from bs4 import BeautifulSoup
MONEY_TYPE = {
'US': 'USD',
}
class BSCollector(object):
"""docstring for BSCollector"""
def __init__(self, url, pic_path, product_name=None):
super(BSCollector, self).__init__()
self.url = url
self.product_name = product_name
self.product_name_path = None
html = requests.get(url)
self.soup = BeautifulSoup(html.content)
self.pd = {}
self.attrs = {}
self.pic_path = pic_path
self.summary = None
self.category_name = None
self.trade_information = {}
self.rich_descs = None
# get product name if not provided in init func
if not self.product_name:
self.product_name = self.soup.find('h1', class_="fn").text.strip()
self.product_name_path = valid_path(self.product_name)
def collect_trade_information(self):
table = self.soup.select('.btable')[0]
names = table.select('.name')
values = table.select('.value')
names = [i.text.strip(':') for i in names]
values = [i.text.strip() for i in values]
raw_info = dict(zip(names, values))
for k, v in raw_info.iteritems():
if 'Min.Order Quantity' == k:
minOrderQuantity = v.split(' ')
self.trade_information['minOrderQuantity'] = minOrderQuantity[0]
self.trade_information['minOrderUnit'] = minOrderQuantity[1]
elif 'FOB Price' == k and v != 'Get Latest Price':
price = v.replace('Get Latest Price', '').split(' ')
self.trade_information['moneyType'] = MONEY_TYPE[price[0]]
self.trade_information['priceRangeMin'] = price[1].strip('$')
self.trade_information['priceRangeMax'] = price[3].strip()
self.trade_information['priceUnit'] = price[6]
elif 'Port' == k:
self.trade_information['port'] = v
elif 'Payment Terms' == k:
self.trade_information['paymentMethod'] = v.strip()
elif 'Supply Ability' == k:
sa = v.strip().split(' ')
self.trade_information['supplyQuantity'] = sa[0]
self.trade_information['supplyUnit'] = sa[1]
self.trade_information['supplyPeriod'] = sa[-1]
if 'minOrderQuantity' not in self.trade_information:
self.trade_information['minOrderQuantity'] = '1'
self.trade_information['minOrderUnit'] = 'Set/Sets'
if 'Port' not in self.trade_information:
self.trade_information['port'] = 'Shenzhen'
if 'Payment Terms' not in self.trade_information:
self.trade_information['paymentMethod'] = 'T/T,Western Union,MoneyGram,PAYPAL'
if 'supplyQuantity' not in self.trade_information:
self.trade_information['supplyQuantity'] = '100'
self.trade_information['supplyUnit'] = 'Set/Sets'
self.trade_information['supplyPeriod'] = 'Week'
def collect_specifications(self):
self.summary = self.soup.find('p', class_='description').text
def collect_attrs(self):
attrs_names = self.soup.find_all('span', class_='attr-name J-attr-name')
attrs_values = self.soup.find_all('td', class_='value J-value')
if len(attrs_names) != len(attrs_values):
raise Exception('collected attribute is not paired')
names = [i.text.strip().replace('.', '') for i in attrs_names]
values = [i.text.strip() for i in attrs_values]
self.attrs = dict(zip(names, values))
# collect category name
cns = self.soup.select('.category')
cns = [cn.text for cn in cns]
self.category_name = ' '.join(cns[2:])
def collect_pics(self):
path = os.path.join(self.pic_path, self.product_name_path)
if not os.path.exists(path):
os.makedirs(path)
# main picture
main_img = self.soup.find('img', class_='photo pic J-pic')
src = main_img.get('src').replace('_250x250.jpg', '')
abs_path = os.path.join(path, self.product_name_path + '.jpg')
self.save_pic(abs_path, src)
# description pictures
imgs = self.soup.select('#J-rich-text-description noscript img')
for i, img in enumerate(imgs):
src = img['src']
if src.endswith('.jpg'):
abs_path = os.path.join(path, self.product_name_path + str(i) + '.jpg')
self.save_pic(abs_path, src)
def collect_package_delivery(self):
# package & delivery
h3 = self.soup.find('h3', text='Packaging & Delivery')
if h3:
table = h3.find_next_sibling('table')
tds = table.select('td')
for i, td in enumerate(tds):
if i % 2 == 0:
self.pd.update({td.text:tds[i+1].text})
else:
self.pd.update({'Packaging Detail:':'Standard Shipping Package'})
self.pd.update({'Delivery Detail:':'3 Working Days Usually'})
def collect_rich_descs(self):
div = self.soup.find('div', id='J-rich-text-description')
div = BeautifulSoup(div.prettify())
elements = div.find_all(['p', 'table', 'ul'])
html = []
for ele in elements:
if 'p' == ele.name and not ele.find_parents('table'):
if ele.find_all('span', text=re.compile('\S+')):
p_html = str(ele).strip().replace('\n', '').replace("'","\\'")
html.append(p_html)
if len(ele.text) > 40:
html.append('<br /><br />')
else:
html.append('<br />')
elif ele.name in ['table', 'ul']:
table_html = str(ele).strip().replace('\n', '').replace("'","\\'")
html.append(table_html)
html.append('<br />')
self.rich_descs = ''.join(html)
with file('t.html', 'wb') as f:
print >> f, self.rich_descs
def collect_key_words(self):
# url = 'http://hz.my.data.alibaba.com/industry/.json?action=CommonAction&iName=searchKeywords&'
pass
def collect(self):
self.collect_specifications()
self.collect_rich_descs()
self.collect_package_delivery()
self.collect_attrs()
self.collect_trade_information()
self.collect_pics()
self.collect_rich_descs()
# self.collect_key_words()
def save_pic(self, name, url):
bin = requests.get(url)
with open(name, 'wb+') as f:
f.write(bin.content)
def collect_names(product_name):
search_url = 'http://www.alibaba.com/trade/search?fsb=y&IndexArea=product_en&CatId=&SearchText='
url = search_url + product_name.replace(' ', '+')
html = requests.get(url)
soup = BeautifulSoup(html.content)
hrefs = soup.select('h2 a')
names = [a['title'] for a in hrefs]
return names
def valid_path(product_name):
# remove special character to avoid bad path
stable = {ord(i):None for i in '/\:*?"<>|'}
return unicode(product_name).translate(stable)
def db_name(email):
return email.replace('@', 'at').replace('.', 'dot')
if __name__ == '__main__':
# url ='http://www.alibaba.com/product-detail/TDP1-5-TDP5-TDP6-Tablet-Making_1892266532.html'
# bsc = BSCollector(url, 'e:/ppictures')
# bsc.collect_rich_descs()
collect_names('http://www.alibaba.com/trade/search?fsb=y&IndexArea=product_en&CatId=&SearchText=pneumatic+pet+strapping+machine')