京东 淘抢购代码

本文介绍了一款用于自动抢购商品的脚本,通过定时触发和页面元素操作实现快速抢购,包括预约、秒杀、结算等流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

var nIntervId;
var count = 1;
var goDate;
function go() {
    console.log("小桃子 ^_^ 正在帮你抢购************* 刷新" + count + "次");
    //console.log("host:" + window.location.hostname);
    count++;
    // iPhone X go set time
    if (Date.now() >= new Date("2017-11-03 15:59:59")) {
        console.log("开始抢购iPhone X" + Date.now());

        // iphoneX 抢购
        if ($(parent.frames[0].document).find("#choose-btn-ko").length == 1) {
            console.log("(++++++++++++iphoneX 抢购");
            var sku = window.location.pathname.replace(/[^0-9]/ig, "");
            var ref = "//cart.jd.com/gate.action?pid=" + sku + "&pcount=1&ptype=1";
            console.log("https:" + ref);
            //5089237
            $(parent.frames[0].document).find("#choose-btn-ko").attr("href", ref);//                 
            parent.frames[0].document.getElementById("choose-btn-ko").click();
            return;
        }

        //预约抢购
        if ($(parent.frames[0].document).find("#btn-reservation").length == 1) {
            console.log("(++++++++++++正在预约抢购");

            parent.frames[0].document.getElementById("btn-reservation").click();
            return;
        }

        //秒杀   
        if ($(parent.frames[0].document).find("#InitCartUrl").length == 1) {
            console.log("(++++++++++++正在秒杀");
            parent.frames[0].document.getElementById("InitCartUrl").click();
            return;
        }

        //去购物车结算
        if ($(parent.frames[0].document).find("#GotoShoppingCart").length == 1) {
            console.log("(++++++++++++正在去购物车结算");
            parent.frames[0].document.getElementById("GotoShoppingCart").click();
        }

        //去结算        
        if ($(parent.frames[0].document).find(".submit-btn").length == 1) {
            console.log("(++++++++++++正在去结算");

            //只提交我抢购的商品
            //var sku = window.location.pathname.replace(/[^0-9]/ig, "");                 

            //$("#toggle-checkboxes_up").trigger("click");
            //全不选择
            //parent.frames[0].document.getElementById("toggle-checkboxes_up").click();

            //$(parent.frames[0].document).find('input:checkbox').attr("checked",false);
            //$(parent.frames[0].document).find("input:checkbox[value^='"+sku+"']").trigger("click");

            //$(parent.frames[0].document).find("input:checkbox[value^='"+sku+"']").attr("checked",true);

            parent.frames[0].document.getElementsByClassName("submit-btn")[0].click();
        }
        //提交订单order-submit
        if ($(parent.frames[0].document).find("#order-submit").length == 1) {
            console.log("(++++++++++++正在提交订单");
            //$(parent.frames[0].document).find(".payment-item item-selected online-payment")

            //在线支付
            parent.frames[0].document.getElementById("order-submit").click();
        }
    }
}
function rewrite(current) {
    fr4me = '<frameset cols=\'*\'>\n<frame src=\'' + current + '\'/>';
    fr4me += '</frameset>';
    with (document) { write(fr4me); void (close()) };
}


//注入sql
rewrite(window.location.href);

//这里需要注意的是,prompt有两个参数,前面是提示的话,后面是当对话框出来后,在对话框里的默认值
var d = prompt("请输入抢购开始时间", "2017-11-03 15:59:59");
//如果返回的有内容
if (d) {
    try {
        goDate = new Date(d);
        console.log("设定时间成功:" + goDate);

        alert("监控期间,请保持标签页在最前面");
        //go(); 0.25秒执行一次
        nIntervId = setInterval("go()", 250);
    }
    catch (e) {
        alert("时间格式不正确,请使用yyyy-MM-dd hh:mm:ss格式,精确到秒, 请重试");
    }
}
else {
    alert("请抢购时间, 请重重试");

}


/*
    clearInterval(nIntervId);//停止监控
    */
--------------------- 
作者:行者无疆-Kevin 
来源:优快云 
原文:https://blog.youkuaiyun.com/zhangzeshuai/article/details/78435766 
版权声明:本文为博主原创文章,转载请附上博文链接!
### Python 实现京东自动抢购商品脚本 为了实现京东平台上商品的自动抢购功能,可以利用 `requests` 和 `selenium` 库来模拟浏览器行为以及处理登录验证等问题。下面提供了一个简化版本的示例代码,该代码展示了如何构建一个基本框架来进行京东商品的自动化抢购。 #### 准备工作 安装必要的库: ```bash pip install requests selenium webdriver-manager ``` #### 示例代码 ```python from selenium import webdriver from selenium.webdriver.common.by import By import time from datetime import datetime from webdriver_manager.chrome import ChromeDriverManager def login_jd(driver, username, password): driver.get('https://passport.jd.com/new/login.aspx') # 输入用户名密码并提交表单 user_input = driver.find_element(By.NAME, 'loginname') pwd_input = driver.find_element(By.NAME, 'nloginpwd') user_input.send_keys(username) pwd_input.send_keys(password) submit_btn = driver.find_element(By.ID, "loginsubmit") submit_btn.click() while True: try: nickname = driver.find_element(By.CLASS_NAME, 'nickname').text print(f'Login success! Welcome {nickname}') break except Exception as e: print('Waiting for QR code scan...') time.sleep(1) def buy_item_at_time(item_url, target_time_str='2023-12-31 23:59:59'): options = webdriver.ChromeOptions() options.add_argument('--disable-blink-features=AutomationControlled') driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=options) item_id = item_url.split('/')[-1].split('.')[0] # 登录账号 login_jd(driver, '<your_username>', '<your_password>') # 进入商品页面 driver.get(item_url) # 设置目标时间戳 target_time = int(time.mktime(datetime.strptime(target_time_str, "%Y-%m-%d %H:%M:%S").timetuple())) current_timestamp = lambda: int(round(time.time() * 1000)) while True: now_ts = current_timestamp()/1000 if now_ts >= target_time: try: # 尝试点击立即购买按钮 buy_now_button = driver.find_element(By.LINK_TEXT,'立即购买') buy_now_button.click() # 提交订单 confirm_order_button = driver.find_element(By.ID,"order-submit") confirm_order_button.click() print("Order placed successfully!") break except Exception as ex: print(ex) continue else: remaining_seconds = (target_time - now_ts) print(f'{remaining_seconds:.2f} seconds until start.') time.sleep(min(max((target_time-now_ts)-1, 0), 0.1)) if __name__ == '__main__': url = 'https://item.jd.com/<product_id>.html' buy_item_at_time(url, '2024-01-01 00:00:00') ``` 此段代码实现了如下几个核心功能: - 使用 Selenium WebDriver 来控制 Chrome 浏览器实例化对象。 - 完成京东网站上的用户登录过程[^1]。 - 设定特定的时间点作为抢购启动时刻,在到达设定时间后执行抢购逻辑。 - 当检测到当前时间为预定抢购时间时,尝试快速定位并点击“立即购买”按钮,并最终确认下单操作[^4]。 请注意,实际应用中还需要考虑更多细节问题,比如异常情况下的重试机制、验证码识别等复杂场景。此外,由于电商平台的安全策略可能随时调整,因此建议定期更新维护此类程序以适应最新的网页结构和技术防护措施。
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值