import requests
from lxml import etree
class Github(object):
def init(self):
# 属性 公共变量 配置项 全局需要访问
self.login_url = 'https://github.com/login'
self.do_login_url = 'https://github.com/session'
self.profile_url = 'https://github.com/settings/profile'
self.headers = {
}
self.s = requests.Session()
def get_csrf_token(self):
"""
请求表单页,获取authenticity_token(csrf token)
:return: {str} 'lkweufjLKSJDIW*#LSKJ'
"""
resp = self.s.get(self.login_url)
html_content = resp.text
pattern = '//input[@name="authenticity_token"]/@value'
dom = etree.HTML(html_content)
authenticity_token = dom.xpath(pattern)[0]
return authenticity_token
def do_login(self, authenticity_token):
"""
去登录
:param authenticity_token: csrf token 组装请求参数所需
:return: True
"""
params = {
'authenticity_token': authenticity_token,
}
resp = self.s.get()
resp.status_code
print("登录成功")
return True
def request_profile(self):
"""
请求个人设置页
:return: {str} '<html>...</html>'
"""
# 请求个人页
return resp.text
def get_user_email(self, html_content):
return email
def get_user_name(self, html_content):
return user_name
# 获取自己的仓库列表
# 获取个人收藏夹仓库列表
if name == ‘main‘:
github = Github()
authenticity_token = github.get_csrf_token()
github.do_login(authenticity_token)
profile_html_content = request_profile()
email = get_user_email(profile_html_content)
print(email)
网友评论