Python模擬Github登陸,詳情請(qǐng)查看源碼點(diǎn)鏈接進(jìn)入Python-Spiders文集,模擬Github登陸可以分為五個(gè)操作步驟,步驟如下:
模擬Github登陸步驟:
1、請(qǐng)求頭:self.headers,請(qǐng)求url;
2、設(shè)置session,保存登陸信息cookies,生成github_cookie文件;
3、POST表單提交,請(qǐng)求數(shù)據(jù)格式post_data;
4、authenticity_token獲?。? 5、在個(gè)人中心驗(yàn)證判斷是否登陸成功,輸出個(gè)人中心信息即登陸成功。
一、獲取請(qǐng)求頭
① 在瀏覽器中敲入https://github.com/login,同時(shí)右擊頁(yè)面查看檢查,如下圖所示:

github_login1.png
② 點(diǎn)擊紅框內(nèi)
login進(jìn)入如下圖所示:
github_login2.png
③ 源碼中對(duì)應(yīng)部分:
# 設(shè)置請(qǐng)求頭
self.headers = {
'Referer': 'https://github.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
'Host': 'github.com'
}
二、保存登陸信息cookies
① 設(shè)置session
② 保存登陸信息cookies,生成github_cookie文件,用cookies保存的信息加載個(gè)人設(shè)置,驗(yàn)證是否模擬登錄成功
③ 源碼中對(duì)應(yīng)部分:
# 設(shè)置session
self.session = requests.session()
# 生成github_cookie文件
self.session.cookies = cookielib.LWPCookieJar(filename='github_cookie')
三、POST表單提交
① POST表單提交字段獲取,如下圖所示:

github_login3.png
② 源碼中對(duì)應(yīng)部分:
登陸時(shí)表單提交參數(shù)
Form Data:
commit:Sign in
utf8:?
authenticity_token:yyZprIm4aghZ0u7r25ymZjisfTjGdUAdDowD9fKHM0oUvHD1WjUHbn2sW0Cz1VglZWdGno543jod2M8+jwLv6w==
login:*****
password:******
四、authenticity_token獲取
① 在瀏覽器中敲入https://github.com/login,獲取網(wǎng)頁(yè)文本文件
② 源碼中對(duì)應(yīng)部分:
# 獲取authenticity_token
def get_token(self):
response = self.session.get(self.loginUrl, headers=self.headers)
html = etree.HTML(response.text)
authenticity_token = html.xpath('//div/input[2]/@value')
print(authenticity_token)
return authenticity_token
五、在個(gè)人中心驗(yàn)證判斷是否登陸成功
在個(gè)人中心驗(yàn)證判斷是否登陸成功,輸出個(gè)人中心信息即登陸成功,如下圖:

github_login4.png
即模擬GitHub模擬登陸成功。
注意:Python-Spiders文集中收錄了較多的爬取文件練習(xí)資料,后期也會(huì)陸續(xù)更新新的Python-Spiders學(xué)習(xí)資料,共同學(xué)習(xí),一起進(jìn)步,喜歡的小伙伴請(qǐng)Star和Fork哦,對(duì)Python-Spiders不懂的小伙伴可以私信我哦,探討和學(xué)習(xí)。
附源碼:
# encoding: utf-8
'''
模擬Github登陸步驟:
1、請(qǐng)求頭:self.headers,請(qǐng)求url
2、設(shè)置session,保存登陸信息cookies,生成github_cookie文件
3、POST表單提交,請(qǐng)求數(shù)據(jù)格式post_data
4、authenticity_token獲取
5、在個(gè)人中心驗(yàn)證判斷是否登陸成功,輸出個(gè)人中心信息即登陸成功
'''
import requests
from lxml import etree
try:
import cookielib
except:
import http.cookiejar as cookielib
class GithubLogin():
def __init__(self):
# url
self.loginUrl = 'https://github.com/login'
self.postUrl = 'https://github.com/session'
self.profileUrl = 'https://github.com/settings/profile'
# 設(shè)置請(qǐng)求頭
self.headers = {
'Referer': 'https://github.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
'Host': 'github.com'
}
# 設(shè)置session
self.session = requests.session()
# 生成github_cookie文件
self.session.cookies = cookielib.LWPCookieJar(filename='github_cookie')
'''
登陸時(shí)表單提交參數(shù)
Form Data:
commit:Sign in
utf8:?
authenticity_token:yyZprIm4aghZ0u7r25ymZjisfTjGdUAdDowD9fKHM0oUvHD1WjUHbn2sW0Cz1VglZWdGno543jod2M8+jwLv6w==
login:*****
password:******
'''
def post_account(self, email, password):
post_data = {
'commit': 'Sign in',
'utf8': '?',
'authenticity_token': self.get_token()[0],
'login': email,
'password': password
}
response = self.session.post(self.postUrl, data=post_data, headers=self.headers)
# 保存cookies
self.session.cookies.save()
def load_cookie(self):
try:
self.session.cookies.load(ignore_discard=True)
except:
print('cookie 獲取不成功')
# 獲取authenticity_token
def get_token(self):
response = self.session.get(self.loginUrl, headers=self.headers)
html = etree.HTML(response.text)
authenticity_token = html.xpath('//div/input[2]/@value')
print(authenticity_token)
return authenticity_token
# 判斷是否登陸成功
def isLogin(self):
self.load_cookie()
response = self.session.get(self.profileUrl, headers=self.headers)
selector = etree.HTML(response.text)
flag = selector.xpath('//div[@class="column two-thirds"]/dl/dt/label/text()')
info = selector.xpath('//div[@class="column two-thirds"]/dl/dd/input/@value')
textarea = selector.xpath('//div[@class="column two-thirds"]/dl/dd/textarea/text()')
# 登陸成功返回來(lái)的個(gè)人設(shè)置信息
print(u'個(gè)人設(shè)置Profile標(biāo)題: %s'%flag)
print(u'個(gè)人設(shè)置Profile內(nèi)容: %s'%info)
print(u'個(gè)人設(shè)置Profile內(nèi)容: %s'%textarea)
if __name__ == "__main__":
github = GithubLogin()
# 輸入自己email賬號(hào)和密碼
github.post_account(email='******', password='******')
# 驗(yàn)證是否登陸成功
github.isLogin()