博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
监测京东商品价格波动
阅读量:5952 次
发布时间:2019-06-19

本文共 4109 字,大约阅读时间需要 13 分钟。

写着玩的,不用再去每天看要买的商品是否降价,如果降价就发布一条推文。

#!/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"}]

转载于:https://www.cnblogs.com/IvanChen/p/5512400.html

你可能感兴趣的文章
InfoQ就Spring Boot 2.0 GA版发布采访了项目牵头人Phil Webb
查看>>
Oracle开源Fn,加入Serverless之争
查看>>
美团即时物流的分布式系统架构设计
查看>>
微软发布.NET Core Tools 1.0版本
查看>>
PHP常用函数之字符串处理
查看>>
又拍云沈志华:如何打造一款安全的App
查看>>
Rust 1.27支持SIMD
查看>>
“10%时间”:优点和缺点——敏捷海滩会议上Elizabeth Pope的报告
查看>>
关于“Fluent UI”或Ribbon Design报道的补充说明
查看>>
2018年终盘点:“年度爆款”的区块链真的结束了吗?
查看>>
51信用卡 Android自动埋点实践
查看>>
敏捷现状10周年调查
查看>>
PHP Session可能会引起并发问题
查看>>
AI产品开发的核心原则:以研究为核心驱动
查看>>
大规模集群中Docker镜像如何分发管理?试试Uber刚开源的Kraken
查看>>
WinDocks发布Windows版本的Docker引擎
查看>>
完美解决html中select的option不能隐藏的问题。
查看>>
推荐5大开源工具,用于开发Kubernetes项目
查看>>
制定2015年的移动开发策略
查看>>
JPA 2.2改进了易用性
查看>>