Skip to content

Commit e25e7e2

Browse files
authored
Merge pull request #11 from LittleYe233/main
Add support for `--dry-run` option and CLI
2 parents 660502b + 38ad769 commit e25e7e2

File tree

3 files changed

+140
-88
lines changed

3 files changed

+140
-88
lines changed

README.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212

1313
由爬虫先行对商品的既有评价进行爬取,在此基础上进行自己的评价
1414

15-
## 使用方法
15+
## 用法
1616

1717
### <请先确保python版本为最新版>
1818

19+
### 快速使用
20+
1921
在终端中执行:
2022

2123
```bash
@@ -34,10 +36,30 @@ user:
3436
最后运行 `auto_comment_plus.py` :
3537

3638
```bash
37-
python auto_comment_plus.py
39+
python3 auto_comment_plus.py
40+
```
41+
42+
**注意:** 请根据设备环境换用不同的解释器路径,如 `python`、`py`。
43+
44+
### 命令行参数
45+
46+
本程序支持命令行参数:
47+
48+
```text
49+
usage: auto_comment_plus.py [-h] [--dry-run]
50+
51+
optional arguments:
52+
-h, --help show this help message and exit
53+
--dry-run have a full run without comment submission
3854
```
3955

40-
> 此脚本需要的依赖较多,需自行安装
56+
**`-h`, `--help`:**
57+
58+
显示帮助文本。
59+
60+
**`--dry-run`:**
61+
62+
完整地运行程序,但不实际提交评论。
4163

4264
## 声明
4365

auto_comment_plus.py

Lines changed: 87 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# @Author : @qiu-lzsnmb and @Dimlitter
44
# @File : auto_comment_plus.py
55

6+
import argparse
67
import random
78
import time
89

@@ -14,12 +15,30 @@
1415
import jdspider
1516

1617

18+
# constants
1719
CONFIG_PATH = './config.yml'
20+
ORDINARY_SLEEP_SEC = 10
21+
SUNBW_SLEEP_SEC = 5
22+
REVIEW_SLEEP_SEC = 10
23+
SERVICE_RATING_SLEEP_SEC = 15
24+
25+
26+
# parse arguments
27+
parser = argparse.ArgumentParser()
28+
parser.add_argument('--dry-run',
29+
help='have a full run without comment submission',
30+
action='store_true')
31+
args = parser.parse_args()
32+
opts = {
33+
'dry_run': args.dry_run
34+
}
1835

1936

37+
# logging
2038
jieba.setLogLevel(jieba.logging.INFO)
2139

2240

41+
# parse configurations
2342
with open(CONFIG_PATH, 'r', encoding='utf-8') as f:
2443
cfg = yaml.safe_load(f)
2544
ck = cfg['user']['cookie']
@@ -43,23 +62,26 @@
4362
'Accept-Encoding': 'gzip, deflate, br',
4463
'Accept-Language': 'zh-CN,zh;q=0.9',
4564
}
46-
# 评价生成
47-
def generation(pname, _class=0, _type=1):
65+
66+
67+
# 评价生成
68+
def generation(pname, _class=0, _type=1, opts=None):
69+
opts = opts or {}
4870
items = ['商品名']
4971
items.clear()
5072
items.append(pname)
5173
for item in items:
5274
spider = jdspider.JDSpider(item)
53-
#增加对增值服务的评价鉴别
54-
if "赠品" in pname or "非实物" in pname or "增值服务" in pname :
75+
# 增加对增值服务的评价鉴别
76+
if "赠品" in pname or "非实物" in pname or "增值服务" in pname:
5577
result = ["赠品挺好的。",
56-
"很贴心,能有这样免费赠送的赠品!",
57-
"正好想着要不要多买一份增值服务,没想到还有这样的赠品。",
58-
"赠品正合我意。",
59-
"赠品很好,挺不错的。"
60-
]
78+
"很贴心,能有这样免费赠送的赠品!",
79+
"正好想着要不要多买一份增值服务,没想到还有这样的赠品。",
80+
"赠品正合我意。",
81+
"赠品很好,挺不错的。"
82+
]
6183
else:
62-
result = spider.getData(4, 3) #这里可以自己改
84+
result = spider.getData(4, 3) # 这里可以自己改
6385

6486
# class 0是评价 1是提取id
6587
try:
@@ -74,19 +96,20 @@ def generation(pname, _class=0, _type=1):
7496
num = 6
7597
elif _type == 0:
7698
num = 4
77-
if len(result) < num :
99+
if len(result) < num:
78100
num = len(result)
79101
else:
80-
num = num
102+
num = num
81103
for i in range(num):
82-
comments = comments + result.pop(random.randint(0, len(result) - 1))
83-
104+
comments = comments + \
105+
result.pop(random.randint(0, len(result) - 1))
84106

85107
return 5, comments.replace("$", name)
86108

87109

88110
# 查询全部评价
89-
def all_evaluate():
111+
def all_evaluate(opts=None):
112+
opts = opts or {}
90113
N = {}
91114
url = 'https://club.jd.com/myJdcomments/myJdcomment.action?'
92115
req = requests.get(url, headers=headers)
@@ -104,7 +127,8 @@ def all_evaluate():
104127

105128

106129
# 普通评价
107-
def ordinary(N):
130+
def ordinary(N, opts=None):
131+
opts = opts or {}
108132
Order_data = []
109133
req_et = []
110134
for i in range((N['待评价订单'] // 20) + 1):
@@ -131,7 +155,7 @@ def ordinary(N):
131155

132156
print(f"\t{i}.开始评价订单\t{oname}[{oid}]")
133157
url2 = "https://club.jd.com/myJdcomments/saveProductComment.action"
134-
xing, Str = generation(oname)
158+
xing, Str = generation(oname, opts=opts)
135159
print(f'\t\t评价内容,星级{xing}:', Str)
136160
data2 = {
137161
'orderId': oid,
@@ -141,14 +165,16 @@ def ordinary(N):
141165
'saveStatus': '1',
142166
'anonymousFlag': '1'
143167
}
144-
pj2 = requests.post(url2, headers=headers, data=data2)
145-
time.sleep(10)
168+
if not opts.get('dry_run'):
169+
pj2 = requests.post(url2, headers=headers, data=data2)
170+
time.sleep(ORDINARY_SLEEP_SEC)
146171
N['待评价订单'] -= 1
147172
return N
148173

149174

150175
# 晒单评价
151-
def sunbw(N):
176+
def sunbw(N, opts=None):
177+
opts = opts or {}
152178
Order_data = []
153179
for i in range((N['待晒单'] // 20) + 1):
154180
url = (f'https://club.jd.com/myJdcomments/myJdcomment.action?sort=1'
@@ -188,20 +214,22 @@ def sunbw(N):
188214
'imgs': imgurl,
189215
'saveStatus': 3
190216
}
191-
req_url2 = requests.post(url2, data={
192-
'orderId': oid,
193-
'productId': pid,
194-
'imgs': imgurl,
195-
'saveStatus': 3
196-
}, headers=headers)
217+
if not opts.get('dry_run'):
218+
req_url2 = requests.post(url2, data={
219+
'orderId': oid,
220+
'productId': pid,
221+
'imgs': imgurl,
222+
'saveStatus': 3
223+
}, headers=headers)
197224
print('完成')
198-
time.sleep(5)
225+
time.sleep(SUNBW_SLEEP_SEC)
199226
N['待晒单'] -= 1
200227
return N
201228

202229

203230
# 追评
204-
def review(N):
231+
def review(N, opts=None):
232+
opts = opts or {}
205233
req_et = []
206234
Order_data = []
207235
for i in range((N['待追评'] // 20) + 1):
@@ -226,23 +254,25 @@ def review(N):
226254
pid, oid = _id.replace(
227255
'http://club.jd.com/afterComments/productPublish.action?sku=',
228256
"").split('&orderId=')
229-
_ , context = generation(oname,_type=0)
257+
_, context = generation(oname, _type=0, opts=opts)
230258
print(f'\t\t追评内容:{context}')
231-
req_url1 = requests.post(url1, headers=headers, data={
232-
'orderId': oid,
233-
'productId': pid,
234-
'content': bytes(context, encoding="gbk"),
235-
'anonymousFlag': 1,
236-
'score': 5
237-
})
259+
if not opts.get('dry_run'):
260+
req_url1 = requests.post(url1, headers=headers, data={
261+
'orderId': oid,
262+
'productId': pid,
263+
'content': bytes(context, encoding="gbk"),
264+
'anonymousFlag': 1,
265+
'score': 5
266+
})
238267
print('完成')
239-
time.sleep(10)
268+
time.sleep(REVIEW_SLEEP_SEC)
240269
N['待追评'] -= 1
241270
return N
242271

243272

244273
# 服务评价
245-
def Service_rating(N):
274+
def Service_rating(N, opts=None):
275+
opts = opts or {}
246276
Order_data = []
247277
req_et = []
248278
for i in range((N['服务评价'] // 20) + 1):
@@ -277,53 +307,56 @@ def Service_rating(N):
277307
'ro899': f'899A{random.randint(4, 5)}', # 快递员服务
278308
'ro900': f'900A{random.randint(4, 5)}' # 快递员服务
279309
}
280-
pj1 = requests.post(url1, headers=headers, data=data1)
310+
if not opts.get('dry_run'):
311+
pj1 = requests.post(url1, headers=headers, data=data1)
281312
print("\t\t", pj1.text)
282-
time.sleep(15)
313+
time.sleep(SERVICE_RATING_SLEEP_SEC)
283314
N['服务评价'] -= 1
284315
return N
285316

286317

287-
def No():
318+
def No(opts=None):
319+
opts = opts or {}
288320
print()
289-
N = all_evaluate()
321+
N = all_evaluate(opts)
290322
for i in N:
291323
print(i, N[i], end="----")
292324
print()
293325
return N
294326

295327

296-
def main():
328+
def main(opts=None):
329+
opts = opts or {}
297330
print("开始京东批量评价!")
298-
N = No()
331+
N = No(opts)
299332
if not N:
300333
print('Ck出现错误,请重新抓取!')
301334
exit()
302335
if N['待评价订单'] != 0:
303336
print("1.开始普通评价")
304-
N = ordinary(N)
305-
N = No()
337+
N = ordinary(N, opts)
338+
N = No(opts)
306339
if N['待晒单'] != 0:
307340
print("2.开始晒单评价")
308-
N = sunbw(N)
309-
N = No()
341+
N = sunbw(N, opts)
342+
N = No(opts)
310343
if N['待追评'] != 0:
311344
print("3.开始批量追评!")
312-
N = review(N)
313-
N = No()
345+
N = review(N, opts)
346+
N = No(opts)
314347
if N['服务评价'] != 0:
315348
print('4.开始服务评价')
316-
N = Service_rating(N)
317-
N = No()
349+
N = Service_rating(N, opts)
350+
N = No(opts)
318351
print("全部完成啦!")
319352
for i in N:
320353
if N[i] != 0:
321354
print("出现了二次错误,跳过了部分,重新尝试")
322-
main()
355+
main(opts)
323356

324357

325358
if __name__ == '__main__':
326359
try:
327-
main()
360+
main(opts)
328361
except RecursionError:
329362
print("多次出现未完成情况,程序自动退出")

0 commit comments

Comments
 (0)