简单使用python(python3.7)模拟登陆github

# !/usr/bin/env python
# -*-coding:utf-8-*-
"""
@Author  : xiaofeng
@Time    : 2018/12/18 16:31
@Desc : Less interests,More interest. (模拟登陆github)
@Project : python_appliction
@FileName: github.py
@Software: PyCharm
@Blog    :https://blog.csdn.net/zwx19921215
"""

import bs4
import requests

# 声明头信息
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0'
}

login_url = 'https://github.com/login'
login_post = 'https://github.com/session'
session = requests.session()

"""
获取authenticity_token
"""


def get_token():
    response = session.get(url=login_url, headers=headers)
    soup = bs4.BeautifulSoup(response.text, 'html.parser')
    # xx = soup.find('input', {'id': 'xxx'}).get('value')
    token = soup.find('input', {'name': 'authenticity_token'}).get('value')
    print(token)
    return token


"""
模拟登陆
"""


def sign_in(username, password):
    form_data = {
        'commit': 'Sign in',
        'utf8': '✓',
        'authenticity_token': get_token(),
        'login': username,
        'password': password
    }

    response = session.post(url=login_post, data=form_data, headers=headers)
    if response.status_code == 200:
        print("congratulations login success ^_^")
        print(response.text)


if __name__ == '__main__':
    sign_in('username', 'password')

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐