-
Notifications
You must be signed in to change notification settings - Fork 3
/
jingdong_crawler.py
792 lines (693 loc) · 27.3 KB
/
jingdong_crawler.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import urllib.request
import re
import time
import random
import smtplib
from email.mime.text import MIMEText
import configparser#read and save configuration.
import ast
import json
import sys
import webbrowser
import codecs
def beep():
"""play an alarm."""
print("\a")
"""
import winsound
Freq = 500 # Set Frequency To 2500 Hertz
Dur = 200 # Set Duration To 1000 ms == 1 second
winsound.Beep(Freq,Dur)
"""
class Settings:
"""
Read settings from jingdong_crawler_setting.ini .
"""
def __init__(self):
self._read_ini()
def _read_ini(self):
self.config=configparser.ConfigParser(delimiters=('='), allow_no_value=True)
with codecs.open('jingdong_crawler_setting.ini', "r", "utf-8") as f:
first = f.read(1)
if first != "\ufeff":
# not a BOM, rewind
f.seek(0)
self.config.read_file(f)
def get_sleep_interval(self):
"""Get interval between every two waves of querying."""
try:
time_str=self.config.get("通用设置", "查询时间间隔秒")
time_str_list=time_str.replace("x", "*").replace("X", "*").split("*")
seconds=1
for i in time_str_list:
seconds=seconds*int(i)
if seconds>0:
return seconds
else:
return 60
except Exception as e:
print(str(e))
return 60
def get_monitoring_addr(self):
try:
MONITORING_ADDR=self.config.get("通用设置", "监控商品网址")
return MONITORING_ADDR
except Exception as e:
print(str(e))
return ""
def is_debug(self):
try:
debug_str=self.config.get("通用设置", "调试")
if debug_str in ("True", "true", "TRUE", "是"):
return True
else:
return False
except Exception as e:
print(str(e))
return False
def get_province_id(self):
try:
iden=self.config.get("收货地址", "省编号")
return int(iden)
except Exception as e:
print(str(e))
return 1
def get_city_id(self):
try:
iden=self.config.get("收货地址", "市编号")
return int(iden)
except Exception as e:
print(str(e))
return 72
def get_county_id(self):
try:
iden=self.config.get("收货地址", "县编号")
return int(iden)
except Exception as e:
print(str(e))
return 2799
def is_query_title(self):
try:
is_query=self.config.get("需要检测变更的内容", "标题")
if is_query in ("True", "true", "TRUE", "是"):
return True
else:
return False
except Exception as e:
print(str(e))
return False
def is_query_detail(self):
try:
is_query=self.config.get("需要检测变更的内容", "描述")
if is_query in ("True", "true", "TRUE", "是"):
return True
else:
return False
except Exception as e:
print(str(e))
return False
def is_query_stock(self):
try:
is_query=self.config.get("需要检测变更的内容", "存货状态")
if is_query in ("True", "true", "TRUE", "是"):
return True
else:
return False
except Exception as e:
print(str(e))
return False
def is_query_promotion(self):
try:
is_query=self.config.get("需要检测变更的内容", "活动")
if is_query in ("True", "true", "TRUE", "是"):
return True
else:
return False
except Exception as e:
print(str(e))
return False
def is_query_coupon(self):
try:
is_query=self.config.get("需要检测变更的内容", "优惠券")
if is_query in ("True", "true", "TRUE", "是"):
return True
else:
return False
except Exception as e:
print(str(e))
return False
def is_query_mobi(self):
try:
is_query=self.config.get("需要检测变更的内容", "移动端优惠")
if is_query in ("True", "true", "TRUE", "是"):
return True
else:
return False
except Exception as e:
print(str(e))
return False
def is_query_price(self):
try:
is_query=self.config.get("需要检测变更的内容", "降价")
if is_query in ("True", "true", "TRUE", "是"):
return True
else:
return False
except Exception as e:
print(str(e))
return False
def is_play_music(self):
try:
is_query=self.config.get("变更播放提示音乐", "播放音乐")
if is_query in ("True", "true", "TRUE", "是"):
return True
else:
return False
except Exception as e:
print(str(e))
return False
def get_music_path(self):
try:
return self.config.get("变更播放提示音乐", "音乐位置")
except Exception as e:
print(str(e))
return ""
def is_send_email(self):
try:
is_send=self.config.get("邮件提醒", "发送邮件提醒")
if is_send in ("True", "true", "TRUE", "是"):
return True
else:
return False
except Exception as e:
print(str(e))
return False
def get_sender_email_server(self):
try:
return self.config.get("邮件提醒", "发件人邮件服务器")
except Exception as e:
print(str(e))
return ""
def get_sender_email_account(self):
try:
return self.config.get("邮件提醒", "发件人邮箱账号")
except Exception as e:
print(str(e))
return ""
def get_sender_email_passwd(self):
try:
return self.config.get("邮件提醒", "发件人邮箱密码")
except Exception as e:
print(str(e))
return ""
def get_receiver_email_account(self):
try:
return self.config.get("邮件提醒", "收件人邮箱账号").split("|")
except Exception as e:
print(str(e))
return ""
settings = Settings()
MAX_PRICE=10000000
#SLEEP_INTERVAL seconds.
SLEEP_INTERVAL=settings.get_sleep_interval()
MONITORING_ADDR=settings.get_monitoring_addr()
DEBUG=settings.is_debug()
PROVINCEID=settings.get_province_id()
CITYID=settings.get_city_id()
COUNTYID=settings.get_county_id()
QUERY_TITLE=settings.is_query_title()
QUERY_DETAIL=settings.is_query_detail()
QUERY_STOCK=settings.is_query_stock()
QUERY_PROMOTION=settings.is_query_promotion()
QUERY_COUPON=settings.is_query_coupon()
QUERY_MOBI=settings.is_query_mobi()
QUERY_PRICE=settings.is_query_price()
PLAY_MUSIC=settings.is_play_music()
MUSIC_PATH=settings.get_music_path()
SEND_EMAIL=settings.is_send_email()
SENDER_MAIL_SERVER=settings.get_sender_email_server()
SENDER_EMAIL_ACCOUNT=settings.get_sender_email_account()
SENDER_EMAIL_PASSWD=settings.get_sender_email_passwd()
RECEIVER_EMAIL_ACCOUNTS=settings.get_receiver_email_account()
class Product:
def __init__(self,id_i):
self.id_i=id_i
self.url="http://item.m.jd.com/product/"+self.id_i+".html?provinceId=%d&cityId=%d&countryId=%d"%(PROVINCEID, CITYID, COUNTYID)
self.page=None
self.html=None
self._compile_regex_object()
#self.load_html()
def _compile_regex_object(self):
regex_title=r'<meta name="keywords" CONTENT="(.+?)">'
regex_price=r'<input type="hidden" id="jdPrice" name="jdPrice" value="(-?[0-9.]+?)"/>'
regex_promotion=r'<div class="prod-act">(.+?)</div>'
regex_detail=r'<div class="act-link">(.+?)<a href'
regex_mobile_discount=r'height="32" width="32">(.+?)[\r\n]'
regex_stock=r'<span class="isExist">(..)'
regex_province=r'<span id="provinceName">(.+?)</span>'
regex_city=r'<span id="cityName">(.+?)</span>'
regex_county=r'<span id="countyName">(.+?)</span>'
self.re_compile_title=re.compile(regex_title)
self.re_compile_price=re.compile(regex_price)
self.re_compile_promotion=re.compile(regex_promotion)
self.re_compile_detail=re.compile(regex_detail)
self.re_compile_mobile_discount=re.compile(regex_mobile_discount)
self.re_compile_stock=re.compile(regex_stock)
self.re_compile_province=re.compile(regex_province)
self.re_compile_city=re.compile(regex_city)
self.re_compile_county=re.compile(regex_county)
def load_html(self):
try:
self.page=urllib.request.urlopen(self.url)
except Exception as e:
print(str(e))
beep()
print(time.ctime())
print("无法下载网页。1秒后重试...")
time.sleep(1)
self.load_html()
return None
self.html=self.page.read()
self.page.close()
def _parse_content(self,re_compile_obj):
"""get content according to each compiled obj that passed in."""
found=re.findall(re_compile_obj,self.html.decode("utf-8"))
try:
return re.sub('<.*?>', '', found[0])
except IndexError:
if DEBUG:
date_time = time.strftime("%Y%m%d_%H%M%S",time.localtime())
html_file = open("jingdong_错误_未找到信息_%s.html"%date_time,"wb")
html_file.write(self.html)
html_file.close()
return None
def get_title(self):
title = self._parse_content(self.re_compile_title)
if title == None:
return "无标题"
else:
return title.strip()
def get_price(self):
try:
return float(self._parse_content(self.re_compile_price))
except TypeError:
beep()
print("无法获取价格...")
time.sleep(4)
return MAX_PRICE
def get_detail(self):
detail = self._parse_content(self.re_compile_detail)
if detail == None:
return "无描述"
else:
return detail.strip()
def get_promotion(self):
promotion = self._parse_content(self.re_compile_promotion)
if promotion == None:
return "无促销"
else:
return promotion.strip()
def get_mobile_discount(self):
mobile_discount = self._parse_content(self.re_compile_mobile_discount)
if mobile_discount == None:
return "无手机端优惠"
else:
return mobile_discount.strip()
def get_coupon(self):
"""Use post method to query whether there is a coupon."""
#Communication procedure seen on httptrace extension in Chrome.
headers = {"Connection": "keep-alive", "Content-Type": "application/x-www-form-urlencoded"}
req = urllib.request.Request(url = 'http://item.m.jd.com/coupon/coupon.json', data = bytes("wareId=%s"%self.id_i, "ascii"), headers = headers, method = "POST")
response = urllib.request.urlopen(req)
try:
#Escape the " for eval use.
content = json.loads(response.read().decode("utf-8"))["coupon"].replace("true", "\"true\"").replace("false", "\"false\"")
except KeyError:
content = "{}"
response.close()
content = ast.literal_eval(content)
coupon_text = ""
if not content:
return "无券"
for coupon in content:
coupon_text = coupon_text+str(coupon["discount"])+"满"+str(coupon["quota"])+coupon["name"]+"____"
return coupon_text.strip()
def get_stock_status(self):
stock = self._parse_content(self.re_compile_stock)
if stock == None:
return "无货"
elif stock == "该地":
return "不送"
else:
return stock.strip()
def get_address(self):
#Address shown on Jingdong goods page. Can be different from the address setted in setting file if setted wrong.
try:
address = self._parse_content(self.re_compile_province)+\
self._parse_content(self.re_compile_city)+\
self._parse_content(self.re_compile_county)
except TypeError:
# unsupported operand type(s) for +: 'NoneType' and 'NoneType'
address = ""
return address.strip()
class Result:
"""
Read/save goods info from/to jingdong_crawler_result.txt.
"""
def __init__(self):
self._read_ini()
def _read_ini(self):
self.config = configparser.RawConfigParser()
self.config.read('jingdong_crawler_result.txt')
def get_url(self, good_id):
try:
return self.config.get(good_id, 'url')
except Exception as e:
print(str(e))
return ''
def set_url(self, good_id, _str):
string = str(_str)
with open('jingdong_crawler_result.txt', 'w') as configfile:
if not self.config.has_section(good_id): self.config.add_section(good_id)
self.config.set(good_id, 'url', string)
self.config.write(configfile)
def get_prev_title(self, good_id):
try:
return self.config.get(good_id, 'name')
except Exception as e:
print(str(e))
return '无法读取标题'
def set_prev_title(self, good_id, _str):
string = str(_str)
with open('jingdong_crawler_result.txt', 'w') as configfile:
if not self.config.has_section(good_id): self.config.add_section(good_id)
self.config.set(good_id, 'name', string)
self.config.write(configfile)
def get_prev_price(self, good_id):
try:
return float(self.config.get(good_id, 'price'))
except Exception as e:
print(str(e))
return MAX_PRICE
def set_prev_price(self, good_id, num):
string = str(num)
with open('jingdong_crawler_result.txt', 'w') as configfile:
if not self.config.has_section(good_id):
self.config.add_section(good_id)
self.config.set(good_id, 'price', string)
self.config.write(configfile)
def get_prev_stock(self, good_id):
try:
return self.config.get(good_id, 'stock')
except Exception as e:
print(str(e))
return '无法读取库存'
def set_prev_stock(self, good_id, string):
with open('jingdong_crawler_result.txt', 'w') as configfile:
if not self.config.has_section(good_id):
self.config.add_section(good_id)
self.config.set(good_id, 'stock', string)
self.config.write(configfile)
def get_prev_promo(self, good_id):
try:
return self.config.get(good_id, 'promotion')
except Exception as e:
print(str(e))
return '无法读取促销信息'
def set_prev_promo(self, good_id, string):
with open('jingdong_crawler_result.txt', 'w') as configfile:
if not self.config.has_section(good_id):
self.config.add_section(good_id)
self.config.set(good_id, 'promotion', string)
self.config.write(configfile)
def get_prev_detail(self, good_id):
try:
return self.config.get(good_id, 'detail')
except Exception as e:
print(str(e))
return '无法读取描述'
def set_prev_detail(self, good_id, string):
with open('jingdong_crawler_result.txt', 'w') as configfile:
if not self.config.has_section(good_id):
self.config.add_section(good_id)
self.config.set(good_id, 'detail', string)
self.config.write(configfile)
def get_min_price(self, good_id):
try:
return float(self.config.get(good_id, 'min_price'))
except Exception as e:
print(str(e))
return MAX_PRICE
def set_min_price(self, good_id, num):
string = str(num)
with open('jingdong_crawler_result.txt', 'w') as configfile:
if not self.config.has_section(good_id):
self.config.add_section(good_id)
self.config.set(good_id, 'min_price', string)
self.config.write(configfile)
def get_prev_mobi(self, good_id):
try:
return self.config.get(good_id, 'mobi')
except Exception as e:
print(str(e))
return '无法读取手机促销信息'
def set_prev_mobi(self, good_id, string):
with open('jingdong_crawler_result.txt', 'w') as configfile:
if not self.config.has_section(good_id):
self.config.add_section(good_id)
self.config.set(good_id, 'mobi', string)
self.config.write(configfile)
def get_prev_coupon(self, good_id):
try:
return self.config.get(good_id, 'coupon')
except Exception as e:
print(str(e))
return '无法读取本地优惠券信息'
def set_prev_coupon(self, good_id, string):
with open('jingdong_crawler_result.txt', 'w') as configfile:
if not self.config.has_section(good_id):
self.config.add_section(good_id)
self.config.set(good_id, 'coupon', string)
self.config.write(configfile)
def internet_on():
"""Check if computer is online. Return type: boolean."""
try:
response=urllib.request.urlopen("http://www.baidu.com",timeout=100)
return True
except Exception as e:
print(str(e))
return False
def send_mail(sub,content,to_list=RECEIVER_EMAIL_ACCOUNTS):
mail_host=SENDER_MAIL_SERVER
mail_user=SENDER_EMAIL_ACCOUNT #用户名
mail_pass=SENDER_EMAIL_PASSWD #口令
me=mail_user
msg = MIMEText(content,_subtype='plain',_charset='UTF-8')
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list)
try:
server = smtplib.SMTP()
server.connect(mail_host)
server.login(mail_user,mail_pass)
server.sendmail(me, to_list, msg.as_string())
server.close()
print("邮件发送成功!")
return True
except Exception as e:
print(str(e))
print("正在检查网络连接...")
if True==internet_on():
print("网络畅通,或许账号或密码有误。程序终止以避免频繁尝试登陆邮箱。")
raise NameError("账号密码有误,请检查。")
else:
print("网络故障,稍后重试...")
return False
def Run():
#Counting querying times.
count=0
#JD goods id.
ID=re.findall(r"/([\d]+).ht", MONITORING_ADDR)
ID=list(set(ID))
while(True):
#Endless querying with sleep interval, until user quit the program.
count+=1
#Sum up all changes in a single query and print at the end of the prompt.
change_print=""
#Introduce randomness to simulate human clicking. Avoiding IP ban from jingdong.
random.shuffle(ID)
#For each link setted in file.
for id_i in ID:
time.sleep(random.randint(100,200)/1000)
url_i = "http://item.m.jd.com/product/"+id_i+".html?provinceId=%d&cityId=%d&countryId=%d"%(PROVINCEID, CITYID, COUNTYID)
print("++++++++++++++++++++++++++++++++++++++++++++++++++++")
product=Product(id_i)
result=Result()
prev_url=result.get_url(id_i)
prev_price=result.get_prev_price(id_i)
prev_title=result.get_prev_title(id_i)
prev_promo=result.get_prev_promo(id_i)
prev_detail=result.get_prev_detail(id_i)
prev_mobi=result.get_prev_mobi(id_i)
prev_stock=result.get_prev_stock(id_i)
prev_coupon=result.get_prev_coupon(id_i)
minium_price=result.get_min_price(id_i)
news_type=""
product.load_html()
curr_price=product.get_price()
curr_title=product.get_title()
curr_promo=product.get_promotion()
curr_detail=product.get_detail()
curr_mobi=product.get_mobile_discount()
curr_stock=product.get_stock_status()
curr_coupon=product.get_coupon()
address = product.get_address()
#save url to file if url not saved:
if prev_url!=url_i:
result.set_url(id_i, url_i)
#record minium price:
if curr_price<minium_price:
#update minium price to file.
minium_price=curr_price
result.set_min_price(id_i, minium_price)
########################################################
#title changed:
if curr_title!=prev_title:
news_type="" #"题"
print("标题!")
change_print+=url_i+"\n"+curr_title[0:10]+": "+curr_title+"\n\n"
result.set_prev_title(id_i, curr_title)
########################################################
#discription changed:
if curr_detail!=prev_detail:
if QUERY_DETAIL:
news_type="描"
print("描述!")
change_print+=url_i+"\n"+curr_title[0:10]+": "+curr_detail+"\n\n"
result.set_prev_detail(id_i, curr_detail)
########################################################
#stock status:
if curr_stock!=prev_stock and curr_stock == "有货":
if QUERY_STOCK:
news_type="货"
print("库存变化!")
change_print+=url_i+"\n"+curr_title[0:10]+": "+curr_stock+"\n\n"
if curr_stock!=prev_stock:
result.set_prev_stock(id_i, curr_stock)
########################################################
#promotion:
if curr_promo!=prev_promo:
if QUERY_PROMOTION:
news_type="活"
print("活动!")
#print(curr_promo.encode("utf-8"))
#print(prev_promo.encode("utf-8"))
change_print+=url_i+"\n"+curr_title[0:10]+": "+curr_promo+"\n\n"
result.set_prev_promo(id_i, curr_promo)
########################################################
#coupon censor:
if curr_coupon!=prev_coupon and curr_coupon != "无券":
if QUERY_COUPON:
news_type="券"
print("优惠券!")
change_print+=url_i+"\n"+curr_title[0:10]+": "+curr_coupon+"\n\n"
if curr_coupon!=prev_coupon:
result.set_prev_coupon(id_i, curr_coupon)
########################################################
#mobile or jd_app price:
if curr_mobi!=prev_mobi:
#print(bytes(curr_mobi, "utf-8"))
#print(bytes(prev_mobi, "utf-8"))
if QUERY_MOBI:
news_type="移"
print("移动端!")
change_print+=url_i+"\n"+curr_title[0:10]+": "+curr_mobi+"\n\n"
result.set_prev_mobi(id_i, curr_mobi)
########################################################
#price drop:
if curr_price<prev_price:
print(prev_price)
print(curr_price)
if QUERY_PRICE:
news_type="降"
print("降! %.1f"%(prev_price-curr_price))
change_print+=url_i+"\n"+curr_title[0:10]+": "+str(curr_price)+"降! %.1f"%(prev_price-curr_price)+"\n\n"
if curr_price!=prev_price:
result.set_prev_price(id_i, curr_price)
#detection finished.
########################################################
message = u"地址 :"+address+"\n"+ \
u"物品 :"+curr_title+"\n"+ \
u"描述 :"+curr_detail+"\n"+ \
u"移动端价格:"+str(curr_price)+"\n"+ \
u"移动端优惠:"+str(curr_mobi)+"\n"+ \
u"有货否 :"+curr_stock+"\n"+ \
u"活动 :"+curr_promo+"\n"+ \
u"优惠券 :"+curr_coupon+"\n" \
u"曾经最低价:"+str(minium_price)+"\n" \
u"网址 :"+url_i+"\n"
if news_type:
print("检测到变更!!!")
if SEND_EMAIL:
#Try 10 times if unabled to send email.
for i in range(10):
if send_mail("%s%d%s"%(news_type,curr_price,curr_title), message):
news_type=""
break;
else:
beep()
print("无法发送。30秒后重试...")
time.sleep(30)
if PLAY_MUSIC:
webbrowser.open(MUSIC_PATH)
else:
print("无变更。")
print(message)
print("##############################################################")
#print out current time.
print(time.ctime())
print("第"+str(count)+"次 汇总结果:")
if ""==change_print:
print("无变更。")
else:
print(change_print)
#Randomness added to sleep interval, simulating human clicking and refreshing.
RAND_INT = random.randint(-5,5)
SLEEP_INTERVAL_RANDOM=int(SLEEP_INTERVAL+RAND_INT)
if SLEEP_INTERVAL_RANDOM < 1:
SLEEP_INTERVAL_RANDOM=1
print("等待 %d+%d=%d秒 更新..."%(SLEEP_INTERVAL, RAND_INT, SLEEP_INTERVAL_RANDOM))
print("如数字不跳动,请按ESC来退出选择模式。")
#Countdown shown on the same position on prompt.
count_length = len(str(SLEEP_INTERVAL_RANDOM))
for i in range(SLEEP_INTERVAL_RANDOM,0,-1):
sys.stdout.write(str(i)+" "+"\r")
time.sleep(1)
sys.stdout.flush()
print("正在获取网页...\n\n")
if __name__ == '__main__':
while True:
try:
Run()
except Exception as e:
print(e)
beep()
time.sleep(5)
'''
#Price: another way to obtain
import requests
pids=1217524
url="http://p.3.cn/prices/mgets?skuIds=J_1217524"# + str(pids)
ret=requests.get(url)
print(ret)
print(ret.content)
'''
r'''
HACK:
ConfigParser, SafeConfigParser and RawConfigParser react with the no escaped "%" differently:
invalid interpolation syntax in '【北京市家电节能补贴 最高补贴13%!】' at position 17
Traceback (most recent call last):
ValueError: invalid interpolation syntax in '【北京市家电节能补贴 最高补贴13%!】' at position 17
'''