码迷,mamicode.com
首页 > 编程语言 > 详细

【Python】京东商品价格监控

时间:2017-05-25 01:12:14      阅读:584      评论:0      收藏:0      [点我收藏+]

标签:class   pytho   find   内蒙古   name   bre   origin   .text   ret   

import requests,json,re,winsound,time

class Stock(object):
    def __init__(self):
        self.province_dict={value:key for key,value in {
            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:‘台湾‘,84:‘钓鱼岛‘,52993:‘港澳‘
        }.items()}
        self.price_dict={
            ‘PC端‘:‘https://p.3.cn/prices/get?type=1&skuid=J_{}‘,
            ‘APP端‘:‘https://pm.3.cn/prices/mgets?origin=2&skuIds={}‘,
            ‘微信端‘:‘https://pe.3.cn/prices/mgets?origin=5&skuids={}‘,
            ‘QQ端‘:‘https://pe.3.cn/prices/mgets?origin=4&skuids={}‘
        }
        self.set_skuId()

    def set_skuId(self):
        self.skuId=input(‘请输入商品ID:‘)
        r=requests.get(‘http://item.jd.com/{}.html‘.format(self.skuId)).text
        cer=re.compile(‘<title>(.*)</title>‘,flags=0)
        self.skuName=cer.findall(r)
        if not self.skuName:
            print(‘您输入的商品ID有误!‘)
            return self.set_skuId()
        else:
            print(self.skuName[0])

    def get_price(self):
        for key,value in self.price_dict.items():
            #print(value.format(self.skuId))
            r=json.loads(requests.get(value.format(self.skuId)).text)[0][‘p‘]
            print(key,r)


    def get_stock(self):
        provinceName=input(‘请输入要查询的省份(如福建):‘)
        if provinceName in self.province_dict.keys():
            r=json.loads(requests.get(‘https://c0.3.cn/stock?skuId={skuId}&cat=1316,1385,1408&area={province}_2805_2855‘.format(skuId=self.skuId,province=str(self.province_dict[provinceName]))+‘&extraParam={%22originid%22:%221%22}‘).text)
            print(format(skuId=self.skuId,province=str(self.province_dict[provinceName])))
            print(r[‘stock‘][‘area‘][‘provinceName‘],‘:‘,r[‘stock‘][‘StockStateName‘])
            if r[‘stock‘][‘StockStateName‘]==‘无货‘:
                flag=input(‘商品暂时无货,是否循环监控?y/n:‘)
                if flag==‘y‘:
                    while True:
                        r=json.loads(requests.get(‘https://c0.3.cn/stock?skuId={skuId}&cat=1316,1385,1408&area={province}_2805_2855‘.format(skuId=self.skuId,province=str(self.province_dict[provinceName]))+‘&extraParam={%22originid%22:%221%22}‘).text)
                        print(r[‘stock‘][‘area‘][‘provinceName‘],‘:‘,r[‘stock‘][‘StockStateName‘])
                        if r[‘stock‘][‘StockStateName‘]==‘现货‘:
                            for i in range(4):
                                winsound.Beep(800,250)
                                print(r[‘stock‘][‘area‘][‘provinceName‘],‘:‘,r[‘stock‘][‘StockStateName‘])
                                time.sleep(1)
                            time.sleep(0.3)
                            winsound.Beep(1600,500)
                            break
                        time.sleep(5)
        else:
            print(‘您的输入有误,请重新输入!‘)
            return self.get_stock()
        
if __name__==‘__main__‘:
    s=Stock()
    s.get_price()
    s.get_stock()

  

【Python】京东商品价格监控

标签:class   pytho   find   内蒙古   name   bre   origin   .text   ret   

原文地址:http://www.cnblogs.com/zeze/p/6901713.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!