菜鸡源码,专注精品下载!
当前位置:首页 > 建站教程 > 建站知识

使用Python编写的淘抢购助手

发布时间:2024-01-05  栏目:建站知识   浏览:   分类:python教程 短链接

Python编写的淘宝抢购小帮手是一种自动化脚本,用于在淘宝网站上自动进行商品抢购。这种脚本通常使用第三方库(如Selenium)来模拟用户操作,如登录、搜索商品、添加到购物车和提交订单等。通过设置定时任务,可以在商品开售前的特定时间自动执行这些操作,从而提高抢购成功的概率。然而,需要注意的是,使用这类脚本可能违反淘宝的使用条款,可能会导致账号被封禁。因此,在使用前请确保了解相关风险并遵守相关规定。

使用方法

1、运行此软件,复制链接到淘宝搜索框内搜索登录;

2、手机淘宝分享需要抢购的链接;

3、填入收获地址的行政区划代码,要抢购的商品SKUID,开始抢购的时间(13位时间戳);

Python代码

importrequests,re,time,execjs,randomimporturllib.parsefromurllib.parseimportunquotedefget_sign(data):##如果方法失效,第一时间要检查JS文件withopen('test.js','r',encoding='utf8')asf:jscode=f.read()sign=execjs.compile(jscode)sign1=sign.call("sign",data)returnsign1def获取参数():短链接=input('请输入微信分享链接:')heard={'User-Agent':'Mozilla/5.0(iPhone;CPUiPhoneOS8_0likeMacOSX)AppleWebKit/600.1.4(KHTML,likeGecko)Mobile/12A365MicroMessenger/5.4.1NetType/WIFI'}r=requests.get(短链接,headers=heard).text真实地址=r[r.find("https://a.m.taobao.com/"):r.find("'",r.find("https://a.m.taobao.com/"))]tk=短链接[短链接.find('=')+1:]ID=真实地址[真实地址.find('/i')+2:真实地址.find('.htm')]price=真实地址[真实地址.find('price=')+6:真实地址.find('&',真实地址.find('price='))]suid=真实地址[真实地址.find('suid=')+5:真实地址.find('&',真实地址.find('suid='))]shareUniqueId=真实地址[真实地址.find('shareUniqueId=')+14:真实地址.find('&',真实地址.find('shareUniqueId='))]ut_sk=真实地址[真实地址.find('ut_sk=')+6:真实地址.find('&',真实地址.find('ut_sk='))]un=真实地址[真实地址.find('un=')+3:真实地址.find('&',真实地址.find('un='))]spm=真实地址[真实地址.find('spm=')+4:真实地址.find('&',真实地址.find('spm='))]sp_tk=真实地址[真实地址.find('sp_tk=')+6:真实地址.find('&',真实地址.find('sp_tk='))]bc_fl_src=真实地址[真实地址.find('bc_fl_src=')+10:真实地址.find('&',真实地址.find('bc_fl_src='))]short_name=真实地址[真实地址.find('short_name=')+11:真实地址.find('&',真实地址.find('short_name='))]bxsign=真实地址[真实地址.find('bxsign=')+7:]heard={'Referer':'https://h5.m.taobao.com/','User-Agent':'Mozilla/5.0(iPhone;CPUiPhoneOS8_0likeMacOSX)AppleWebKit/600.1.4(KHTML,likeGecko)Mobile/12A365MicroMessenger/5.4.1NetType/WIFI'}时间戳=str(int(time.time()*1000))r=requests.get('https://h5api.m.taobao.com/h5/mtop.taobao.baichuan.smb.get/1.0/?jsv=2.6.2&appKey=12574478&t=1641292896089&sign=e02e24da3b819b99a6d7344bb0c310a8&api=mtop.taobao.baichuan.smb.get&v=1.0&type=originaljson&dataType=jsonp&timeout=10000',headers=heard).cookiestoken=r['_m_h5_tk'][:r['_m_h5_tk'].find('_')]data='{'+'"id":"{}","price":"{}","sourceType":"item","suid":"{}","shareUniqueId":"{}","ut_sk":"{}","un":"{}","share_crt_v":"1","un_site":"0","spm":"{}","sp_tk":"{}","bc_fl_src":"{}","cpp":"1","shareurl":"true","short_name":"{}","bxsign":"{}","sm":"{}","app":"weixin","detail_v":"3.5.0","exParams":"'.format(ID,price,suid,shareUniqueId,ut_sk,un,spm,sp_tk,bc_fl_src,short_name,bxsign,tk)+r'{"id":"'+ID+r'","price":"'+price+r'","sourceType":"item","suid":"'+suid+r'","shareUniqueId":"'+shareUniqueId+r'","ut_sk":"'+ut_sk+r'","un":"'+un+r'","share_crt_v":"1","un_site":"0","spm":"'+spm+r'","sp_tk":"'+sp_tk+r'","bc_fl_src":"'+bc_fl_src+r'","cpp":"1","shareurl":"true","short_name":"'+short_name+r'","bxsign":"'+bxsign+r'","sm":"'+tk+r'","app":"weixin","appReqFrom":"detail","container_type":"xdetail","dinamic_v3":"true","supportV7":"true","ultron2":"true"}","itemNumId":"'+ID+'","pageCode":"miniAppDetail","_from_":"miniapp"}'sign=get_sign(token+'&'+时间戳+'&12574478&'+data)url='https://h5api.m.taobao.com/h5/mtop.taobao.detail.getdetail/6.0/?jsv=2.6.2&appKey=12574478&t={}&sign={}&api=mtop.taobao.detail.getdetail&v=6.0&ttid=202012%40taobao_h5_9.17.0&isSec=0&ecode=0&AntiFlood=true&AntiCreep=true&H5Request=true&type=jsonp&dataType=jsonp&callback=mtopjsonp1&data={}'.format(时间戳,sign,urllib.parse.quote(data))#开始获取数据---------------------------------------------r=requests.get(url,headers=heard).textcategoryId=re.findall(r'categoryId":"(.*?)"',r)[0]print(categoryId)sellerId=re.findall(r'sellerId":(.*?),',r)[0]print(sellerId)itemId=re.findall(r'&itemId=(.*?)',r)[0]print(itemId)returnitemId,sellerId,categoryIddefgenerateUmidToken():umid_token='C'+str(int(time.time()*1000))umid_token+=''.join(str(random.choice(range(10)))for_inrange(11))umid_token+=str(int(time.time()*1000))umid_token+=''.join(str(random.choice(range(10)))for_inrange(3))returnumid_tokendef淘宝扫码登录():session=requests.Session()headers={'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/79.0.3945.130Safari/537.36'}login_url='https://login.taobao.com/member/login.jhtml?allp=&wbp=&sub=false&sr=false&c_is_scure=&from=tbTop&type=1&style=&minipara=&css_style=&tpl_redirect_url=https%3A//www.taobao.com&popid=&callback=&is_ignore=&trust_alipay=&full_redirect=&need_sign=&sign=×tamp=&from_encoding=&qrLogin=&keyLogin=&newMini2='getqr_login_url='https://qrlogin.taobao.com/qrcodelogin/generateQRCode4Login.do?adUrl=&adImage=&adText=&viewFd4PC=&viewFd4Mobile=&from=tb&appkey=00000000&umid_token={}'checkqr_login_url='https://qrlogin.taobao.com/qrcodelogin/qrcodeLoginCheck.do?lgToken={}&defaulturl=https://www.taobao.com'session.headers.update(headers)umid_token=generateUmidToken()response=session.get(login_url)response=session.get(getqr_login_url.format(umid_token))response_json=response.json()ifresponse_json['success']:xcode_url=response_json.get('url','')lg_token=response_json.get('lgToken','')print('复制此链接到某宝搜索进行登录:','https://login.m.taobao.com/qrcodeCheck.htm?lgToken={}&tbScanOpenType=Notification'.format(lg_token))else:raiseRuntimeError('Failtologin,unabletofetchurlofqrcode')session.headers.update({'Referer':'https://login.taobao.com/member/login_unusual.htm?user_num_id=2979250577&is_ignore=&from=tbTop&style=&popid=&callback=&minipara=&css_style=&is_scure=true&c_is_secure=&tpl_redirect_url=https%3A%2F%2Fwww.taobao.com%2F&cr=https%3A%2F%2Fwww.taobao.com%2F&trust_alipay=&full_redirect=&need_sign=&#172;_duplite_str=&from_encoding=&sign=×tamp=&sr=false&guf=&sub=false&wbp=&wfl=null&allp=&loginsite=0&login_type=11&lang=zh_CN&appkey=00000000&#182;m=7nmIF0VTf6m%2Bbx8wuCmPLTEdh1Ftef8%2B5yUA%2FXNtAI%2FfMwadkeaCast40u2Ng0%2FC7Z75sOSVLMugWTqKjJ7aA55JYIL%2FPDFJ7zaJhq9XSVUOX%2B1AxQatuIvw4TXGJm1VG4alZ2UohVAAt5WTLYbs5im077nTG%2BOkovORQNtMCEzWKMe0xcuienFAhsBhC0V7qIYZJvPGOOEt0tORA8Fv1zYPuOkWEPDFsPwYG5xj4LTKNZt5HSRRHkviiPy9AJ9uC%2Bs7V%2FQ7b6K07YUG1fA3tFwALGnorSUXRdhcXUBBAt6IiyStIkWFWDgJEymOAXOS5RNGlO1EL5ppmpQas7BarrW2Krui4bxV81AJXyxLfnk3MOxI2dUNdO9VQNY0F6a6nk%2FCzUfR0NfPRrIoXuZDn2N01A8q5XGrMlWmBCH5%2FSKz6%2F%2BrUx3%2FxQTYWmgV49rVSdtySIHip5PsrXHWXCbHqscdve540l5CUKTT7znsoL45pth%2FosxMUb649Yw1EPAq'})whileTrue:response=session.get(checkqr_login_url.format(lg_token))response_json=response.json()#--扫码成功ifresponse_json['code']=='10006':#----检查是否需要安全验证response=session.get(response_json.get('url','')+'&umid_token={}'.format(umid_token))ifresponse.url.find('login_unusual.htm')>-1:raiseRuntimeError('Failtologin,youraccountrequiressecurityverification')uid,token=re.findall(r'uid=(.*?)&token=(.*?)&',response_json.get('url'))[0]username=unquote(uid.replace('cntaobao',''))break#--二维码已经失效elifresponse_json['code']=='10004':raiseRuntimeError('Failtologin,qrcodehasexpired')#--正在扫码或其他原因elifresponse_json['code']in['10001','10000']:passtime.sleep(1)cookies_dict=requests.utils.dict_from_cookiejar(session.cookies)print('[INFO]:Account->%s,loginsuccessfully'%username)infos_return={'username':username,'uid':uid,'token':token}infos_return.update(response_json)print(cookies_dict)returncookies_dictcookies=淘宝扫码登录()tb_token=cookies['_tb_token_']tsid=cookies['t']itemId,sellerId,categoryId=获取参数()#https://m.tb.cn/h.fQxjOfl?tk=Igyd2bCwOtMdeliveryCityCode=input('请输入您当地的邮政编码:')#行政区划代码比如汕头就是440500skuId=input('请输入商品SKUID:')#'4440144444848'heard={'referer':'https://detail.tmall.com/item.htm?&id={}&skuId={}'.format(itemId,skuId),'user-agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/97.0.4692.99Safari/537.36',}抢购时间=input('请输入抢购时间:')whileTrue:print(type(抢购时间))r=requests.get('http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp').text时间戳=int(r[-16:-3])ifint(抢购时间)<=时间戳:#ks=str(time.time()*1000)ks=时间戳r=requests.get('https://fbuy.tmall.com/cart/addCartItems.do?_tb_token_={}&add=%7B%22deliveryCityCode%22%3A{}%2C%22campaignId%22%3A0%2C%22from_etao%22%3A%22%22%2C%22umpkey%22%3A%22%22%2C%22items%22%3A%5B%7B%22itemId%22%3A%22{}%22%2C%22skuId%22%3A%22{}%22%2C%22iChannel%22%3A%22%22%2C%22quantity%22%3A1%2C%22serviceInfo%22%3A%22%22%2C%22extraAttribute%22%3A%7B%7D%7D%5D%7D&tsid={}&itemId={}&sellerId={}&categoryId={}&root_refer=&item_url_refer=&noAnim=true&_ksTS={}_787&callback=jsonp788'.format(tb_token,deliveryCityCode,itemId,skuId,tsid,itemId,sellerId,categoryId,ks),headers=heard,cookies=cookies)print(r.text)breakprint('时间未到,继续等待')time.sleep(0.5)


评论
建站知识
建站知识
使用技巧
调试安装
运营推广