老师,并没有返回成功,代码应该是和老师一样的呀?哪里出错了

来源:3-18 设置状态保持session(一)

MasirYXM

2020-05-09 18:40:36

import requests
import re
import time

index_url = 'http://account.chinaunix.net/login'
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
"Connection": "keep-alive",
"Cookie": "XSRF-TOKEN=heqZpSHbxoWLOSzmIGGByeOJwV0Pjsd9HAqC8o3V; laravel_session=QtcSymHExMrcQGB30aFHxOOIcV4ZZ1GSuiln4N93; account_chinauni=accountchinauni; reg_referer=account.chinaunix.net; captcha_gee=5eb6721c856d1; st_user_token=3a0da693cb4b356f469f8a69ceecae0e",
"Host": "account.chinaunix.net",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36",
}

login_session = requests.session()
token_search = re.compile(r'name="_token"\svalue="(.*?)"')
index_response = login_session.get(url=index_url, headers=headers)
token_value = re.search(token_search, index_response.text).group(1)
print(token_value)
data = {
   'username': '13689098099',
   'password': 'cnhnq@0328',
   '_token': token_value,
   '_t': int(time.time())
}
login_url = 'http://account.chinaunix.net/login/login'
login_response = login_session.post(url=login_url, headers=headers, data=data)
print(login_response.text)

phone_url = 'http://account.chinaunix.net/ucenter/user/index'
phone_response = login_session.get(url=phone_url, headers=headers)
print(phone_response.text)

最后结果

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no">

    <meta name="renderer" content="webkit">

        <title>登录</title>

    <link rel="stylesheet" href="/static/css/style.css">

</head>

<body class="bg">

<h2 class="logo-cu"></h2>

    <script>

        //172.16.98.168

        //223.87.205.177

        var needverify=0;

    </script>

    <div class="box">

        <div class="nav"><a href="/login" class="here"> 登录 <i></i> </a> <a href="/register"> 注册 <i></i> </a>

        </div>

        <form class="popup" method="post">

            <ul class="list">

                <li class="form-item">

                    <div class="inp_box">

                        <div class="inp_box_1"><span class="user"></span>

                            <input value="" placeholder="用户名/手机号" id="username" maxlength="36" type="text" class="inp">

                        </div>

                    </div>

                    <div class="error hide" for="username"></div>

                </li>

                <li class="form-item">

                    <div class="inp_box">

                        <div class="inp_box_1"><span class="password"></span>

                            <input value="" placeholder="密码" id="password" maxlength="36" type="password" class="inp">

                        </div>

                    </div>

                    <div class="error hide" for="password"></div>

                </li>

                <li class="slide_code" id="slide_code" style="display: none;">

                    <div class="inp_box inp_box_gee">

                        <div id="embed-captcha"></div>

                        <p id="wait" class="show">正在加载验证码......</p>

                        <p id="notice" class=hide>请先完成验证</p>

                    </div>

                    <div class="error hide" for="embed-captcha"></div>

                </li>

                <li>

                    <div class="forgot"><a href="/forgotPassword/phone" target="_self">找回密码</a></div>

                </li>

                <li><input class="submit-btn" value="登录" id="btnSubmit" type="button"></li>

            </ul>

            <div class="login-qrcode" id="wxlogin" style="display: none;">

            </div>

            <input type="hidden" name="_token" value="heqZpSHbxoWLOSzmIGGByeOJwV0Pjsd9HAqC8o3V">

        </form>

        <div class="title"><span>登录方式</span></div>

        <ul class="login_ways" id="login_ways">

            <li class="phone"><a type="1">手机号登录</a></li>

            <li class="pwd"><a class="here" type="2">密码登录</a></li>

                    </ul>

    </div>

    <script>

        var wx_login_appid = '';

    </script>

    <script src="/static/js/login.js?id=bd2401b1e1243312fbb0"></script>

    <script src="https://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script>

</body>

</html>

并没有我的手机号信息

写回答

1回答

时间,

2020-05-09

同学,你好。同学可以看下当前页面中是否需要输入验证码才可以正常登录。在页面进行登录时会有验证码验证,每次验证码验证是不一样的,因此在访问时也要对其进行验证才可以正常登录。属于反爬策略的一种,同学理解操作原理就可以。在下一节讲解中对该部分进行了提示,并使用了另一个网站学习该内容。

http://img.mukewang.com/climg/5eb68af1090d337d04700223.jpg

如果我的回答解决了您的疑惑,请采纳!祝学习愉快~~~~

0

0 学习 · 1672 问题

查看课程