import requests
from lxml import etree
class Login(object):
def __init__(self):
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'
}
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 token(self):
response = self.session.get(self.login_url,headers=self.headers) #访问登陆页面
selector = etree.HTML(response.text)
token = selector.xpath('//div/input[2]/@value')[0] #XPath解析authenticity_token信息
return token
def login(self,email,password):
post_data = {
'commit':'Sign in',
'utf8': '?',
'authenticity_token': self.token(),
'login': 'email',
'password': password
}
本内容试读结束,登录后可阅读更多
下载后可阅读完整内容,剩余1页未读,立即下载