python 模拟登录github

一直无法接受 python 的缩进语法,所以非常排斥 python, 但是工作中又离不开python, 所以只好接受它 . 接触一两天发现python用起来还是挺方便的(废话)

2020-11-17-01-43-24

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
68
69
70
71
72
73
74
75
76
77
78
79

# -*- coding: UTF-8 -*-
from __future__ import print_function
import requests
import re
import sys


def login(user, password):
"""

:rtype: str
"""
session = requests.session()
headers = {
'Referer': 'https://github.com/',
'Host': 'github.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/86.0.4240.193 Safari/537.36'
}

# 配置代理
proxies = {
"http": "http://127.0.0.1:10809",
"https": "http://127.0.0.1:10809"

}

response = session.get('https://github.com/login', headers=headers, proxies=proxies)
text = response.content.decode("utf8")
token = re.findall(r'name="authenticity_token" value="(.*?)"', text)[0]
timestamp = re.findall(r'timestamp" value="(\d+?)"', text)[0]
timestamp_secret = re.findall(r'timestamp_secret" value="([a-fA-F\d]*?)"', text)[0]

# login
data = {
'commit': 'Sign in',
'authenticity_token': token,
'ga_id': '',
'login': user,
'password': password,
'webauthn-support': 'supported',
'webauthn-iuvpaa-support': 'unsupported',
'return_to': '',
'allow_signup': '',
'client_id': '',
'integration': '',
'required_field_a5b2': '',
'timestamp': timestamp,
'timestamp_secret': timestamp_secret
}

response = session.post('https://github.com/session', data=data, headers=headers, proxies=proxies)
login_info = response.content.decode("utf8")
# print(login_info)
session_info = re.findall('Incorrect username or password.', login_info)
# print(session_info)
if not session_info:
print('[+] login successful!')
else:
print('[-] Incorrect username or password.')
sys.exit(0)

info_url = 'https://github.com/users/%s/pinned_items_modal' % user
response = session.get(info_url, headers=headers, proxies=proxies)
git_info = response.content.decode("utf8")
t = re.findall(
r'data-filter-item-text>[\n\s]+(.*?)[\n\s]+</strong>[\n\s]+<span class="stars no-wrap">[\n\s]+(\d+)[\n\s]+<svg',
git_info)
i = 0
l = len(t)
while i < l:
print(t[i][0], t[i][-1], u'★ ', ' ')
i = i + 1


if __name__ == '__main__':
login('mabangde', 'HECXZ78798798#@!#')
# print(result)