import requests
from lxml import etree
class Login(object):
def __init__(self):
self.headers = {
'Referer': 'https://www.tuicool.com/login',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',
'Host': 'www.tuicool.com'
}
self.login_url = 'https://www.tuicool.com/login'
self.recommand = 'https://www.tuicool.com/a/?lang=0'
self.shoucan = 'https://www.tuicool.com/articles/my'
self.setting = 'https://www.tuicool.com/settings/info'
self.session = requests.session()
def token(self):
response = self.session.get(self.login_url, headers=self.headers)
selector = etree.HTML(response.text)
# todo
token = selector.xpath('//div//input[2]/@value')
new = self.session.get(self.recommand, headers=self.headers)
# print(new.text)
# print('------------------------------------------------')
return token
def login(self, email, password):
post_data = {
'utf8': '✓',
'authenticity_token': self.token()[0],
'email': email,
'password': password,
'remember': 1
}
response = self.session.post(self.login_url, data=post_data, headers=self.headers)
if response.status_code == 200:
print(response.text)
print('+++++++++++++++++++++++++++++++++++++++++++++')
response = self.session.get(self.setting, headers=self.headers)
if response.status_code == 200:
print(response.text)
if __name__ == '__main__':
login = Login()
login.login(email='', password='')