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

简介: 果壳的特殊之处是有隐藏的随机token令牌,登录界面查看源代码:隐藏的令牌import sysimport os.pathimport http.

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

img_b22e494b32345dca09c7c41c5e5050d3.png
登录界面

查看源代码:


img_c2a9e10b7d70b49217d7e5746fa559ad.png
隐藏的令牌
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()
目录
相关文章
|
2月前
|
数据采集 机器学习/深度学习 安全
Python爬虫之极验滑动验证码的识别
了解极验滑动验证码、特点、识别思路、初始化、模拟点击、识别缺口、模拟拖动。
69 0
|
8月前
|
数据采集 文字识别 测试技术
Python3,这个库,真的是图片类型验证码的克星,真香。
Python3,这个库,真的是图片类型验证码的克星,真香。
47 0
|
2月前
|
数据采集 Web App开发 文字识别
Python爬虫之点触验证码的识别
点触验证码识别思路,初始化,获取,识别。
62 0
Python爬虫之点触验证码的识别
|
2月前
|
数据采集 文字识别 开发者
Python爬虫之图形验证码的识别
python爬虫逆向图形验证码分析,处理和测试实战。
50 0
|
2月前
|
机器学习/深度学习 人工智能 文字识别
Python常用验证码标注和识别(需求分析和实现思路)
Python常用验证码标注和识别(需求分析和实现思路)
54 0
|
2月前
|
存储 安全 JavaScript
使用Python的Flask框架开发验证码登录功能
使用Python的Flask框架开发验证码登录功能
33 0
|
6月前
|
Python
Python随机生成验证码的两种方法
Python随机生成验证码的两种方法
|
文字识别 Python
关于利用python进行验证码识别的一些想法
转载请注明:@小五义http://www.cnblogs.com/xiaowuyi         用python加“验证码”为关键词在baidu里搜一下,可以找到很多关于验证码识别的文章。我大体看了一下,主要方法有几类:一类是通过对图片进行处理,然后利用字库特征匹配的方法,一类是图片处理后建立字符对应字典,还有一类是直接利用ocr模块进行识别。
1004 0
|
10天前
|
存储 人工智能 数据处理
Python:编程的艺术与科学的完美交融
Python:编程的艺术与科学的完美交融
14 1