-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathJDprice.py
168 lines (133 loc) · 5.4 KB
/
JDprice.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
import json
import re
from lxml import html
import HTMLInfo
import sys
class JDPrice(object):
def __init__(self, url):
self.url = url
HTMLInfo.REFERER = url
r = HTMLInfo.get_html(url)
self.html = r.text
self.info = self.get_product()
def get_url_page(self):
tree = html.fromstring(self.html)
page = tree.xpath('//div[@id="J_filter"]//div[@id="J_topPage"]//span[@class="fp-text"]/i/text()')
if page:
page = page[0]
else:
print("Error: Cannot get the pages")
sys.exit()
return int(page) if int(page) < 2 else 2
def create_url(self, url_list):
page = self.get_url_page()
for i in range(1, int(page) + 1):
url_list.append(self.url + str(i))
def get_itemlist(self, itemlist):
tree = html.fromstring(self.html)
status = tree.xpath('//div[@id="J_goodsList"]//div[@class="p-img"]//@href')
for item in status:
if re.search('^//item.jd.com', item):
item = re.sub('//', 'https://', item)
if item not in itemlist:
itemlist.append(item)
def get_product(self):
product_pattern = re.compile(r'compatible: true,(.*?)};', re.S)
product_info = re.findall(product_pattern, self.html)
if product_info:
return product_info[0]
return None
def get_product_jpg(self):
jpg_pattern = re.compile(r"src: '(.*?)',")
jpg = "http://img10.360buyimg.com/n1/" + re.findall(jpg_pattern, self.info)[0]
return jpg
def get_product_skuid(self):
sku_id_pattern = re.compile(r'skuid: (.*?),')
sku_id = re.findall(sku_id_pattern, self.info)[0]
return sku_id
def get_product_cate(self):
cat_pattern = re.compile(r"cat: \[(.*?)\],")
cat = re.findall(cat_pattern, self.info)[0]
return cat
def get_vendorId(self):
vid_pattern = re.compile(r'venderId:(.*?),')
vid = re.findall(vid_pattern, self.info)[0]
return vid
def get_shopId(self):
sid_pattern = re.compile(r"shopId:'(.*?)',")
sid = re.findall(sid_pattern, self.info)[0]
return sid
def get_product_promotion(self):
discount = {}
content = ""
vip = ""
sku_id = self.get_product_skuid()
cat = self.get_product_cate()
vender_id = self.get_vendorId()
shop_id = self.get_shopId()
# 2_2813_51976_0 stands for Shanghai; 1_72_2799_0 means Beijing
url = "http://cd.jd.com/promotion/v2?&skuId=" + sku_id + "&area=2_2813_51976_0&shopId=" + shop_id + "&venderId=" + vender_id + "&cat=" + cat
prom = HTMLInfo.get_html(url).content.decode('gbk')
try:
if prom.find('You have triggered an abuse') < 0:
prom = json.loads(prom)
if "skuCoupon" in prom.keys():
if prom["skuCoupon"]:
for i in prom["skuCoupon"]:
discount[i["discount"]] = i["quota"]
if "prom" in prom.keys():
if "tags" in prom["prom"].keys():
if prom["prom"]["tags"]:
if prom["prom"]["tags"][0]["name"] == u'会员特价':
vip = prom["prom"]["tags"][0]["name"]
if "pickOneTag" in prom["prom"].keys():
if prom["prom"]["pickOneTag"]:
content = prom["prom"]["pickOneTag"][0]["content"]
except Exception as ex:
print('get_product_promotion ', ex)
sale = ""
gift = ""
if discount:
for i in discount.keys():
sale += u'满减:满' + str(discount[i]) + u'减' + str(i) + "<br />"
if vip:
vip = str(vip) + "<br />"
if content:
gift = u'满赠:' + str(content) + "<br />"
promotion = vip + sale + gift
return promotion
def get_product_name(self):
name = ""
try:
name_pattern = re.compile(r"name: '(.*?)',")
name = re.findall(name_pattern, self.info)[0]
except Exception as ex:
print(ex)
return bytes(name.encode()).decode('unicode-escape')
def get_product_price(self):
price = ""
plus_price = ""
date = {}
sku_id = self.get_product_skuid()
r = HTMLInfo.get_html("https://d.jd.com/lab/get?callback=lab")
match_pattern = re.compile(r"lab\(\[(.*?)\]\)")
try:
json_data = json.loads(re.findall(match_pattern, r.text)[0])
except Exception as ex:
print('get_product_price Ex:', ex)
if re.match('www.jd.com', json_data['url']):
date = json_data["startOn"]
date = str(date) + "1608370126"
# this url to get the price for JD
url = "http://p.3.cn/prices/mgets?&type=1&pduid=" + date + "&skuIds=J_" + sku_id
# response.json() can return the json-encoded content of a response
status = HTMLInfo.get_html(url).json()[0]
if status:
if 'tpp' in status:
plus_price = u"PLUS价:<br />" + status['tpp']
if 'p' in status:
price = u"京东价:<br />" + status['p']
return price + "<br />" + plus_price
if __name__ == '__main__':
jd = JDPrice("https://item.jd.com/4488334.html")
print(jd.get_product_price())