Github模拟登陆



github 的登录页面没有验证码,而且比较简单,所以先拿 github 练练手。

按照惯例,先规划一下步骤:

1.进入登录页,F12 查看请求,查看必要参数。

2.构造登录函数。

3.爬虫成功登陆后采集信息。


进入登录页真实登录

登录页网址: https://github.com/login

页面如下:

现在先输入正确的账号密码进行登录。

打开 F12 开发工具,打开 Network 标签,先输入正确的账号密码,点击 Sign in,然后观察请求。可以看到,第一条请求是 https://github.com/session , 请求方式是 post 方式,而且 Form Data 里面存在账号密码信息,ok,就是这个请求,如下图。

能看到请求方式为 post 然后看下 Form Data ,其中有这么几个参数,commitutf8authenticity_tokenloginpassword,经过观察,commitutf8loginpassword 这几个都是固定值,而 authenticity_token 每次登录都会发生改变,先找到这个参数是怎么出来的,才方便构造登录函数,经查找后发现在登录页,也就是 https://github.com/login 这个页面,再查看源代码可以查找到这个 token 值,如下图所示。

构造登录函数

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
import requests
from lxml import etree


class github_login(object):

def __init__(self):
self.headers = {
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Mobile Safari/537.36',
}
self.login_url = 'https://github.com/login'
self.post_url = 'https://github.com/session'
self.logined_url = 'https://github.com/settings/profile'
# 为了保持会话,需要用到 requests 的 session 服务
self.session = requests.Session()

# 获取token值
def get_token(self):
html = self.session.get(self.login_url, headers=self.headers)
response = etree.HTML(html.text)
token = response.xpath("//input[@name='authenticity_token']/@value")[0]
return token

# 上面已经获取到了token参数,那么开始写登录函数
# 把username和password作为变量,后期传入
def login(self, token, email, password):
data = {
'commit': 'Sign in',
'utf8': '✓',
'authenticity_token': token,
'login': email,
'password': password
}
response = self.session.post(self.post_url, data=data, headers=self.headers)
if response.status_code == 200:
print('登录成功,正在跳转到个人信息...')

采集信息

上面得到 token 值,也成功登录了 github 。现在我们就可以开始采集信息了。在这里我选择进入个人详情页来获取用户名和地址信息。

1
2
3
4
5
def parse(self, html):
response = etree.HTML(html)
name = response.xpath("//dl[@class='form-group'][1]/dd[1]/input/@value")[0]
location = response.xpath("//dl[@class='form-group'][6]/dd[1]/input/@value")[0]
print('姓名: %s, 地址: %s' % (name, location))

获取到用户名和地址信息。

完整代码

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
import requests
from lxml import etree


class github_login(object):

def __init__(self):
self.headers = {
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Mobile Safari/537.36',
}
self.login_url = 'https://github.com/login'
self.post_url = 'https://github.com/session'
self.logined_url = 'https://github.com/settings/profile'
self.session = requests.Session()

def get_token(self):
html = self.session.get(self.login_url, headers=self.headers)
response = etree.HTML(html.text)
token = response.xpath("//input[@name='authenticity_token']/@value")[0]
return token

def parse(self, html):
response = etree.HTML(html)
name = response.xpath("//dl[@class='form-group'][1]/dd[1]/input/@value")[0]
location = response.xpath("//dl[@class='form-group'][6]/dd[1]/input/@value")[0]
print('姓名: %s, 地址: %s' % (name, location))

def login(self, token, email, password):

data = {
'commit': 'Sign in',
'utf8': '✓',
'authenticity_token': token,
'login': email,
'password': password
}
response = self.session.post(self.post_url, data=data, headers=self.headers)
if response.status_code == 200:
print('登录成功,正在跳转到个人信息...')

response = self.session.get(self.logined_url, headers=self.headers)
self.parse(response.text)


if __name__ == '__main__':
login = github_login()
token = login.get_token()
login.login(token, 'username', 'password')

-------------本文结束感谢您的阅读-------------

本文标题:Github模拟登陆

文章作者:Tang

发布时间:2018年05月01日 - 17:05

最后更新:2019年01月20日 - 18:01

原始链接:https://tangx1.com/github/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%