| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- import re
- import requests as req
- # import time,random,winsound,sendmessage,numpy as np
- import time,random,numpy as np
- from datetime import datetime
- class CS():
- def __init__(self):
- self.nowtime = datetime.now()
- self.goodsurl = 'https://buff.163.com/api/market/goods'
- self.pricehistoryurl = 'https://buff.163.com/api/market/goods/price_history/buff'
- self.my_headers = {
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36",
- "Cookie": "_ntes_nnid=db955d39ec6952987ea31591e7a7f51e,1735042169953; _ntes_nuid=db955d39ec6952987ea31591e7a7f51e; timing_user_id=time_dPeVya6k5c; _ga=GA1.1.1303944876.1735384064; _ga_EPDQHDTJH5=GS1.1.1735384063.1.1.1735384225.0.0.0; Device-Id=qX7ZJINOsL1UQiaTIf95; P_INFO=18505515510|1737427893|1|netease_buff|00&99|CN&1737426178&netease_buff#CN&null#10#0#0|&0|null|18505515510; remember_me=U1084863581|sm9KUY0tjXZMgm0Vq13zmUzDYlPClIym; session=1-sb6kP-_c8kkrLJXbr9rLFXTGPxHDRE3pMgZBLjwFylla2020027141; Locale-Supported=zh-Hans; game=csgo; csrf_token=ImM1NTUzMDA3MjZjZTk4OWFhZDdjZjI4ZGYwMGFkNDg0MDdjNjE0MDki.Z49c_Q.lopzgJuqeS1EURnwRW3X_3T6xtY",#你自己的cookie
- "Host": "buff.163.com",
- "Referer": "https://buff.163.com/market/?game=csgo",
- "X-Requested-With": "XMLHttpRequest"
- }
- def SearchGoods(self, weaponame): # 搜索符合价格标准的weaponlist商品,返回商品id等数据
- nowtime = self.nowtime.timestamp()
- ##############参数调整################
- param = {
- 'game': 'csgo',
- 'page_size': 80,
- 'page_num': 100,
- 'quality':'normal',
- 'min_price': 20, # 最小价格
- 'max_price': 221.93, # 最大价格
- 'category': weaponame,
- }
- datalist = []
- res = req.get(url=self.goodsurl, headers=self.my_headers, params=param)
- print(res.text)
- data = res.json()['data']
- datalist.extend(data['items'])
- #print(data)
- print('商品页数'+str(data['total_page']))
- #buff页码显示有问题
- if int(data['total_page']) > 1:
- for i in range(1, int(data['total_page'])):
- time.sleep(random.randint(10, 20))
- param.update({
- 'page_num': i
- })
- res = req.get(url=self.goodsurl, headers=self.my_headers, params=param)
- data = res.json()['data']
- datalist.extend(data['items'])
- print(weaponame + '数据获取成功') # 打印数据列表
- return datalist
- def GethistoryPrice(self,goodsdata):
- param = {
- 'game': 'csgo',
- 'goods_id':'',
- 'currency': 'CNY',
- 'days':'30',
- 'buff_price_type': '1',
- 'with_sell_num': 'false',
- '_':''
- }
- nowtime = self.nowtime.timestamp()
- param.update({'goods_id':goodsdata['id'],
- '_':nowtime}) # 参数加入id和时间戳
- res = req.get(url=self.pricehistoryurl, headers=self.my_headers, params=param)
- #print(res.content)
- price_history = res.json()['data']['price_history']
- print('历史价格获取成功')
- return price_history
- def OperaData(self,pricelist,nowprice):
- data=[]
- for item in pricelist:
- data.append(item[1])
- # 计算四分位数
- #print(data)
- Q1 = np.percentile(data, 25)
- Q3 = np.percentile(data, 75)
- IQR = Q3 - Q1
- # 剔除超出Q1-1.5*IQR和Q3+1.5*IQR的值
- filtered_data = [x for x in data if Q1 - 1.5 * IQR <= x <= Q3 + 1.5 * IQR]
- #print(filtered_data)
- npdata = np.array(filtered_data)
- # 价格线设置——————————————————————历史价格的8%
- priceline = np.percentile(npdata, 8)
- print('价格线:' + str(priceline))
- print('当前价位:' + str(nowprice))
- weight=1/(float(nowprice)-float(priceline))
- print('权重',str(weight))
- if weight <= 0:
- return 1, float(priceline),100
- else:
- return 2, float(priceline),weight
- def judgepirce(self,item):
- print('★★★★★★★★★★')
- print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
- print(str(item['name']))
- try:
- historypirce = self.GethistoryPrice(item)
- if len(historypirce) > 55:
- buyok, Q1, weight = self.OperaData(historypirce, item['sell_min_price'])
- if buyok==1:
- print('———————————价格较低,可以入手————————————')
- print('https://buff.163.com/goods/'+str(item['id']))
- winsound.PlaySound("output.wav", winsound.SND_FILENAME)
- item.update({
- 'buyok':buyok,
- 'priceline':Q1,
- 'weight':weight
- })
- return item
- else:
- print('交易量过少')
- item.update({
- 'buyok': 3,
- 'weight': 0.01
- })
- return item
- except Exception as e:
- print('获取历史价格失败', str(e))
- item.update({
- 'buyok': 4,
- 'weight': 10
- })
- return item
- if __name__ == '__main__':
- # weaponlist = [ 'weapon_awp','weapon_usp_silencer', 'weapon_ak47', 'weapon_glock','weapon_deagle','weapon_m4a1', 'weapon_m4a1_silencer', ]
- weaponlist = ['weapon_revolver','weapon_bayonet', 'weapon_bizon', 'weapon_sg556', 'weapon_scar20', 'weapon_p250',
- 'weapon_m4a1', 'weapon_cz75a', 'weapon_aug','weapon_usp_silencer',
- 'weapon_xm1014', 'weapon_elite','weapon_sawedoff','weapon_nova','weapon_famas', 'weapon_fiveseven', 'weapon_g3sg1', 'weapon_galilar',
- 'weapon_glock', 'weapon_hkp2000', 'weapon_m249', 'weapon_mac10', 'weapon_mag7',
- 'weapon_mp5sd', 'weapon_mp7', 'weapon_mp9', 'weapon_negev', 'weapon_ssg08', 'weapon_zeus',
- 'weapon_tec9', 'weapon_ump45', 'weapon_ak47', 'weapon_m4a1_silencer', 'weapon_awp', 'weapon_deagle','weapon_p90']
- test = CS()
- list=[]
- for i in weaponlist:#获取商品信息
- data=test.SearchGoods(i)
- list.extend(data)
- time.sleep(random.randint(10, 60))
- while True:#判断商品价格
- for i in list:
- test.judgepirce(i)
- time.sleep(random.randint(10, 60))
|