python 模拟登陆github的示例

时间:2022-05-17 11:40:46
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-
# @Author: CriseLYJ
# @Date:  2020-08-14 12:13:11
 
import re
import requests
 
 
class GithubLogin(object):
 
  def __init__(self, email, password):
    # 初始化信息
    self.headers = {
      'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
      'Referer': 'https://github.com/',
      'Host': 'github.com'
    }
 
    self.session = requests.Session()
    self.login_url = 'https://github.com/login'
    self.post_url = 'https://github.com/session'
    self.email = email
    self.password = password
 
  def login_GitHub(self):
    # 登录入口
    post_data = {
      'commit': 'Sign in',
      'utf8': '✓',
      'authenticity_token': self.get_token(),
      'login': self.email,
      'password': self.password
    }
    resp = self.session.post(
      self.post_url, data=post_data, headers=self.headers)
    
    print('StatusCode:', resp.status_code)
    if resp.status_code != 200:
      print('Login Fail')
    match = re.search(r'"user-login" content="(.*?)"', resp.text)
    user_name = match.group(1)
    print('UserName:', user_name)
 
 
 
  # Get login token
  def get_token(self):
 
    response = self.session.get(self.login_url, headers=self.headers)
 
    if response.status_code != 200:
      print('Get token fail')
      return None
    match = re.search(
      r'name="authenticity_token" value="(.*?)"', response.text)
    if not match:
      print('Get Token Fail')
      return None
    return match.group(1)
 
 
if __name__ == '__main__':
  email = input('Account:')
  password = input('Password:')
 
  login = GithubLogin(email, password)
  login.login_GitHub()

登录效果

python 模拟登陆github的示例

以上就是python 模拟登陆github的示例代码的详细内容,更多关于python 模拟登陆github的资料请关注服务器之家其它相关文章!

原文链接:https://github.com/Kr1s77/awesome-python-login-model