终端效果:
终端截图
代码实现:
import requests
from lxml import etree
class Login(object):
def __init__(self):
self.headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Connection': 'keep-alive',
'Host': 'github.com',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 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()
self.token = self.get_token()
def get_token(self):
response = self.session.get(self.login_url,headers = self.headers)
selector = etree.HTML(response.text)
token = selector.xpath("//input[@name='authenticity_token']/@value")
return token
def login(self):
post_data = {
'utf8':'✓',
'authenticity_token':self.token,
'login':你的账号,
'password':你的密码,
}
response = self.session.post(self.post_url, data=post_data,headers = self.headers)
if response.status_code == 200:
print(response.text)
else:
print(response.status_code)
def main():
login = Login()
login.login()
if __name__ == '__main__':
main()
网友评论