项目背景
项目实现:
- 提取 excel 中的数据
- 使用 requests 模块将数据上传至后台
代码设计
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# -*- coding:utf-8 -*-
"""
@Author : haauleon
@Contact : 753494552@qq.com
@File : upload_product.py
@Date : 2022-05-11 10:56:00
@Function: 将客户提供的 upload 图片目录和 productTemplate.xlsx 文件的商品数据上传至壹壹车后台
1. 处理 productTemplate.xlsx 文件的商品数据并返回
2. 保存商品数据至壹壹车后台商品管理列表
3.注意:
"""
import os
import requests
import json
import xlrd
import logging
import sys
import time
from colorama import Fore, Style
import pprint
print = pprint.pprint
# 壹壹车后台认证签名
AUTHORIZATION = 'xxx'
# 壹壹车后台认证信息
COOKIE = 'xxx'
# 壹壹车后台访问渠道
USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) ' \
'Chrome/90.0.4430.93 Safari/537.36 '
_logger = logging.getLogger('YiYiCar') # 获取日志记录器
_logger.setLevel(logging.DEBUG) # 设置日志等级
_handler = logging.StreamHandler(sys.stdout) # 输入到控制台的 handler
_logger.addHandler(_handler)
def info(msg):
now = time.strftime("%Y-%m-%d %H:%M:%S")
_logger.info(Fore.GREEN + now + " [INFO] " + str(msg) + Style.RESET_ALL)
def error(msg):
now = time.strftime("%Y-%m-%d %H:%M:%S")
_logger.error(Fore.RED + now + " [ERROR] " + str(msg) + Style.RESET_ALL)
def _print(msg):
now = time.strftime("%Y-%m-%d %H:%M:%S")
_logger.debug(Fore.BLUE + now + " [PRINT] " + str(msg) + Style.RESET_ALL)
class ConfigHandler:
_SLASH = os.sep
# 项目路径
root_path = os.path.dirname(os.path.abspath(__file__))
# 图片路径
img_path = os.path.join(root_path, 'upload' + _SLASH + 'upload' + _SLASH + '201704' + _SLASH
+ '0d453758ca6d71c7ab13e943bd4c3b98.jpg')
# 文件路径
excel_path = os.path.join(root_path, 'productTemplate.xlsx')
class ExcelHandler:
"""壹壹车商品清单文件处理"""
def __init__(self, file_path, t_loc=0):
"""默认处理excel中的第一个table"""
self.file_path = file_path
self.xl = xlrd.open_workbook(file_path)
self.table = self.xl.sheets()[t_loc]
def table_nrows(self):
"""获取sheet表的总行数"""
return self.table.nrows
def get_row_value(self):
"""获取每一行的列值"""
for row in range(1, self.table_nrows()):
store_name = self.table.cell(row, 0).value # 商品名称
keyword = self.table.cell(row, 1).value # 商品关键字
image_loc = self.table.cell(row, 2).value # 商品封面图
slider_image = self.table.cell(row, 3).value # 商品轮播图
description = self.table.cell(row, 4).value # 商品详情
price = self.table.cell(row, 5).value # 商品价格
# _print([store_name, keyword, int(image_loc), slider_image, description, '{:.2f}'.format(price*10000)])
yield store_name, keyword, int(image_loc), slider_image, description, '{:.2f}'.format(price*10000)
class YiYiCar:
"""壹壹车后台管理"""
def __init__(self):
self.base_url = 'http://boss.yyc.bringbuys.com'
self.auth = AUTHORIZATION
self.cookie = COOKIE
self.user_agent = USER_AGENT
self.user = requests.Session()
def upload_file(self, file_path: str):
"""上传单个图片
@param file_path: 本地文件路径
@return: 服务器文件路径
"""
upload_url = self.base_url + '/api/upload'
p = {'type': '1'}
file_name = file_path.split('/')[-1]
f = [('file', (file_name, open(file_path, 'rb'), 'image/jpeg'))]
header = {
'Authorization': self.auth,
'Cookie': self.cookie,
'User-Agent': self.user_agent,
}
res = self.user.post(upload_url, headers=header, data=p, files=f)
# _print(res.json()['link'])
return res.json()['link']
def save_product(self, store_name: str, keyword: str, image: str, slider_image: list, description: str, price: str):
"""创建商品
@param store_name: 商品名称
@param keyword: 商品关键字
@param image: 商品封面图
@param slider_image: 商品轮播图
@param description: 商品详情
@param price: 商品价格
"""
payload = json.dumps({
"imageArr": [],
"sliderImageArr": [],
"store_name": store_name,
"cate_id": 24,
"keyword": keyword,
"unit_name": "辆",
"store_info": "",
"image": image,
"slider_image": slider_image,
"description": "<p>%s </p>" % description,
"ficti": 0,
"give_integral": 0,
"sort": 0,
"is_show": 1,
"is_hot": 1,
"is_benefit": 1,
"is_best": 1,
"is_new": 1,
"is_good": 0,
"is_postage": 0,
"is_sub": 0,
"is_integral": 0,
"id": 0,
"spec_type": 0,
"temp_id": 37,
"attrs": [
{
"imageArr": [],
"pic": image,
"price": price,
"cost": price,
"ot_price": price,
"stock": "1",
"seckill_stock": 0,
"seckill_price": 0,
"pink_stock": 0,
"pink_price": 0,
"bar_code": "0",
"weight": 0,
"volume": 0,
"brokerage": 0,
"brokerage_two": 0,
"integral": 0
}
],
"items": [],
"header": [],
"selectRule": ""
})
save_url = self.base_url + '/api/yxStoreProduct/addOrSave'
header = {
'Authorization': self.auth,
'Cookie': self.cookie,
'User-Agent': self.user_agent,
'Content-Type': 'application/json',
}
res = self.user.post(save_url, headers=header, data=payload)
if res.status_code == 201:
_print("商品创建成功: {}".format(store_name))
class YiYiCarRun:
"""执行壹壹车商品批量上传脚本"""
@staticmethod
def run_to_create_products():
"""从excel提取商品信息并创建商品"""
excel = ExcelHandler(ConfigHandler.excel_path)
e = excel.get_row_value()
yyc = YiYiCar()
while True:
# 获取商品数据
store_name, keyword, image_loc, slider_image, description, price = next(e)
try:
new_slider_images = list()
for file_path in slider_image.split('|'):
# 上传图片至服务器
img_path = yyc.upload_file(ConfigHandler.root_path + file_path)
# 组装商品轮播图
new_slider_images.append(img_path)
# 设置商品封面图
image = new_slider_images[image_loc-1]
# 上传商品
yyc.save_product(store_name, keyword, image, new_slider_images, description, price)
except:
# 可能存在图片不存在等异常
error("商品创建失败: {}".format(store_name))
if __name__ == '__main__':
# print(ConfigHandler.root_path)
# print(ConfigHandler.img_path)
# print(ConfigHandler.excel_path)
YiYiCarRun.run_to_create_products()