python3登录网站smzdm.com

时间:2015-08-22 07:42:37

标签: javascript jquery python

我正在学习python3来登录网站

在Chrome中,我发现登录表单为:

<form id="login_form" action="http://www.smzdm.com/user/login" method="post">
    <div class="item-tip">用户名/邮箱</div>
    <input id="user_login" class="form-input" type="text" name="user_login" value="" tabindex="1" autocomplete="off">   
    <div class="item-tip">密码</div>
    <input id="user_pass" class="form-input" type="password" name="user_pass" value="" tabindex="2">
    <input type="submit" id="btn_login" class="btn_login" value="登&nbsp;&nbsp;录" tabindex="4"> 
</form>

但是当我登录并发现他们使用jQuery GET METHOD 来发送数据时:

$("#btn_login").click(function() {
    var l = $(".notice_error");
    var b = $.trim($("#user_login").val());
    var j = $("#user_pass").val();
    var k = $.trim($("#captcha").val());
    var m = $("#rememberme").is(":checked") + 0;
    var e = $.trim($("#is_third").val());
    var d = $("#is_pop_login").val();
    if (b == "") {
        showError(l, "请输入用户名或邮箱");
    } else {
        if (j == "") {
            showError(l, "请输入密码");
        } else {
            if ($(".captcha_switch").is(":visible") && k == "") {
                showError(l, "请输入验证码");
            } else {
                var g = new Base64();
                var o = 0;
                if ($("#rememberme").is(":checked")) {
                    o = 1;
                }
                var c = "";
                if (k == "geetest") {
                    var p = ["geetest_challenge", "geetest_validate", "geetest_seccode"];
                    for (var f in p) {
                        c += "&" + p[f] + "=" + encodeURIComponent($("input[name=" + p[f] + "]").val());
                    }
                }
                var h = $("#user_domain").val() + "/user/login/jsonp_check";
                $.ajax({
                    type: "get",
                    url: h,
                    data: "user_login=" + encodeURIComponent(b) + "&user_pass=" + encodeURIComponent(j) + "&rememberme=" + m + "&is_third=" + e + "&is_pop=" + d + "&captcha=" + k + c,
                    dataType: "jsonp",
                    jsonp: "callback",
                    success: function(r) {
                        var t = r.data.redirect_to;
                        var s = r.data.is_use_captcha;
                        var x = r.error_code;
                        if (t != "" && t != undefined) {
                            window.location.href = t;
                        } else {
                            if (s) {
                                var v = $("#captcha_img").attr("data-src");
                                $("#captcha_img").attr("src", v);
                                $(".captcha_switch").show();
                            }
                            if (x == 0) {
                                if ($("#pop-login").length > 0) {
                                    location.reload();
                                } else {
                                    var q = $.trim($("#redirect_to").val());
                                    if ("" == q) {
                                        q = "http://www.smzdm.com";
                                    }
                                    window.location.href = q;
                                }
                            }
                            var w = r.error_msg;
                            for (var u in w) {
                                showError(l, w[u]);
                                break;
                            }
                        }
                    }

,请求网址为:

http://www.smzdm.com/user/login/jsonp_check?callback=jQuery111006958817734848708_1440206381009&user_login=MYNAME&user_pass=MYPASSWORD&rememberme=0&is_third=&is_pop=1&captcha=&_=1440206381011

之后,我制作了python代码:

url = 'http://www.smzdm.com/user/login/jsonp_check'
request_data = {
    'user_login': 'MYUSERNAME',
    'user_pass': 'MYPASSWORD'
}
headers = {
    'Host': 'www.smzdm.com',
    'Referer': 'http://www.smzdm.com/',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5)'
}
cookie = http.cookiejar.MozillaCookieJar(cookie_file)
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie))
urllib.request.install_opener(opener)
data = urllib.parse.urlencode(request_data).encode('utf-8')
request = urllib.request.Request(url,  headers=headers)
respone = urllib.request.urlopen(request, data=data)
# Another request gets the user's info
req = urllib.request.Request('http://www.smzdm.com/user', headers=headers)
r = urllib.request.urlopen(req)
result = r.read().decode('utf-8')
print(result)

但是在运行这些代码之后我似乎没有成功登录,这是我犯了什么错误吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您错过了大量查询字符串......

request_data = {
'callback'  : 'jQuery111006958817734848708_1440206381009',
'user_login' : 'MYPASSWORD',
'user_pass' : 'MYUSERNAME',
'rememberme' :'0',
'is_third'  : '0',
'is_pop'    : '0',
'captcha' : '',
'_' : '1440206381011'}

请尝试相反,它应与您发布的网址完全匹配。