前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python模拟登陆 —— 征服验证码 4 果壳

Python模拟登陆 —— 征服验证码 4 果壳

作者头像
SeanCheney
发布2018-04-24 10:22:55
8490
发布2018-04-24 10:22:55
举报
文章被收录于专栏:SeanCheney的专栏SeanCheney的专栏

果壳的特殊之处是有隐藏的随机token令牌,

登录界面

查看源代码:

隐藏的令牌

代码语言:javascript
复制
import sys
import os.path
import http.cookiejar

import requests
from bs4 import BeautifulSoup

login_url = ("http://www.guokr.com/sso/"
             "?suppress_prompt=1&lazy=y&success=http%3A%2F%2Fwww.guokr.com%2F")
session = requests.session()
session.cookies = http.cookiejar.LWPCookieJar()
# Opening ``login_url`` will redirect us to the actual page where we
# can retreive some necessary data and make post requests.
resp = session.get(login_url)  # We'll make post requests to ``resp.url``.
soup = BeautifulSoup(resp.text, "html.parser")


def get_csrf_token():
    """
    :rtype: str
    """
    csrf_token = soup.find(id="csrf_token").attrs["value"]
    return csrf_token


def get_captcha_rand():
    """
    :rtype: str
    """
    captcha_rand = soup.find(id="captchaRand").attrs["value"]
    return captcha_rand


def get_captcha_img():
    """
    :rtype: NoneType
    """
    captcha_img_url = soup.find(id="captchaImage").attrs["src"]
    resp = session.get(captcha_img_url, stream=True)
    with open("captcha_img.png", "wb") as f:
        for chunk in resp.iter_content(chunk_size=128):
            f.write(chunk)


def login(url, username, password, csrf_token, captcha, captcha_rand):
    """
    :type url: str
    :type username: str
    :type password: str
    :type csrf_token: str
    :type captcha: str
    :type captcha_rand: str
    :rtype: NoneType
    """
    payload = {
        "username": username,
        "password": password,
        "csrf_token": csrf_token,
        "captcha": captcha,
        "captcha_rand": captcha_rand,
        "permanent": "y"
    }
    try:
        resp = session.post(url, data=payload)
        resp.raise_for_status()
    except requests.exceptions.HTTPError:
        soup = BeautifulSoup(resp.text, "html.parser")
        error = soup.find(class_="login-error")
        sys.exit(error.string.strip())


def is_logged_in():
    """
    :rtype: bool
    """
    url = "http://www.guokr.com/settings/profile/"
    resp = session.get(url)
    if "gheaderSettings" in resp.text:
        return True
    else:
        return False


def main():
    """Run the program."""
    csrf_token = get_csrf_token()
    captcha_rand = get_captcha_rand()
    get_captcha_img()
    username = input("Enter username> ")
    password = input("Enter password> ")
    captcha = input("Enter CAPTCHA ({})> "
                    .format(os.path.abspath("captcha_img")))
    login(resp.url, username, password, csrf_token, captcha, captcha_rand)
    if is_logged_in():
        print("You are now logged in.")
    else:
        print("Hmm... Something is wrong.")
    print("Cookies are save in {}".format(os.path.abspath("cookies")))
    session.cookies.save("cookies", ignore_discard=True)


if __name__ == "__main__":
    main()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017.09.29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档