写着玩的,不用再去每天看要买的商品是否降价,如果降价就发布一条推文。
#!/usr/bin/env python# coding: utf-8import tweepyimport requestsimport reimport sysimport datetimeimport json# 京东的编码是gbkreload(sys)sys.setdefaultencoding('gbk')head = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36" }today = str(datetime.date.today())myfavourite = { # 这里的价格是我期望的价格,非商品实际价格 '789778':99.00, # 汇源100%橙汁200ml*24盒 '1169940':179.00 # 飞利浦(PHILIPS)HX6013/05标准型牙刷头3支装适用牙刷HX6730HX6761HX6511HX3216HX6972HX3130HX3120HX3110}# 通过商品id号获取商品名称def getproductname(productid): url = 'http://item.jd.com/' + productid + '.html' s = requests.session() r = s.get(url, headers=head) pattern_title = re.compile(ur"(.*?)
") result_productname = re.findall(pattern_title, unicode(r.content)) return result_productname# 获取商品详细信息def getproductdetails(productid): url = 'http://pe.3.cn/prices/pcpmgets?skuids=' + productid + '&origin=5&area=15_1213_3409' s = requests.session() r = s.get(url, headers=head).json() return r # p手机端实际价格 pcp电脑端实际价格 # [{u'p': u'168.00', u'pcp': u'188.00', u'm': u'398.00', u'id': u'1456371991'}]# 发布推文def posttwitter(content): consumer_key = 'f...j' consumer_secret = 'W...8' access_token = '4...j' access_token_secret = 'Q...M' auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) if content: api.update_status(status=content)# 判断价格涨跌def detectprice(productid): productdetails = getproductdetails(productid)[0] # {u'p': u'168.00', u'pcp': u'188.00', u'm': u'398.00', u'id': u'1456371991'} result_twitter = '<<' + getproductname(productid)[0] + '>>' + u'降价了!当前手机端价格为:' + productdetails['p'] log = { 'date': today, 'id': productid, 'name': getproductname(productid)[0].encode('utf-8'), 'price': float(productdetails['p'])} result_log = json.dumps(log) if float(productdetails['p']) < myfavourite[productid]: # 如果今天的价格 低于 期望价 with open('log_' + productid + '.txt', "r") as logfile: logfile_content = logfile.readlines()[-1:] # 取最后一行数据,即昨天得价格 if float(productdetails['p']) < json.loads(logfile_content[0])['price']: # 如果今天的价格 低于 昨天的 with open('log_' + productid + '.txt', "a") as logfile: logfile.write(result_log + "\n") posttwitter(result_twitter) return u'推文已发布!' else: # 如果今天的价格 高于或等于 昨天的 with open('log_' + productid + '.txt', "a") as logfile: logfile.write(result_log + "\n") return u"由于今天的价格等于或高于昨天,故不再发布推文通知!" else: # 如果今天的价格 等于或高于 期望价 with open('log_' + productid + '.txt', "a") as logfile: logfile.write(result_log + "\n") return u"实际价格高于或等于您的预期!"# 调用函数print detectprice('789778')
附上京东的几个查询接口:
1. http://p.3.cn/prices/mgets?skuIds=J_1456371991
[{"id":"J_1456371991","p":"188.00","m":"398.00"}]2. http://c0.3.cn/stock?skuId=1456371991&cat=652,829,854&area=15_1213_3409_0&extraParam={"originid":"1"}
{"stock":{"rn":-1,"IsPurchase":false,"ArrivalDate":"","channel":1,"StockState":33,"PopType":0,"vd":null,"isJDexpress":"0","StockStateName":"现货","Dti":null,"afsCode":1,"area":{"countyName":"江干区","success":true,"townName":"","cityName":"杭州市","provinceName":"浙江"},"support":[],"Dc":[{"ordermin":0,"dcash":0,"dtype":1,"freihtType":1}],"dcashDesc":"","sidDely":"-1","rfg":0,"stockDesc":"<strong>有货</strong>","pr":{"promiseResult":null,"resultCode":-55},"code":1,"skuState":1,"Ext":"fare:0,is7ToReturn:1","realSkuId":1456371991,"promiseResult":"","eir":[{"helpLink":"//help.jd.com/user/issue/103-983.html","showName":"货到付款","iconType":0,"resultCode":1,"iconTip":"支持送货上门后再收款,支持现金、POS机刷卡等方式","iconSrc":"货到付款","picUrl":"http://static.360buyimg.com/item/assets/picon/fangzi.png","iconCode":"pop_PaymentCod"}],"ir":[]},"choseSuit":[]}3. http://pe.3.cn/prices/pcpmgets?skuids=1456371991&origin=5&area=15_1213_3409
[{"id":"1456371991","pcp":"188.00","p":"168.00","m":"398.00"}]