def login(self, username="", password=""):
# logging in without credentials
self.username = username
response_page = self.codechef_session.get(CodechefSession.codechef_url)
html_page = lxml.html.fromstring(response_page.text)
hidden_inputs = html_page.xpath(
r'//form//input[@type="hidden"]'
)
payload = {i.attrib["name"]: i.attrib["value"]
for i in hidden_inputs}
payload['name'] = username
payload['pass'] = password
payload['op'] = 'Login'
response = self.codechef_session.post(CodechefSession.codechef_url, data=payload)
# removing extra sessions using simple scraping and form handling
while response.url == CodechefSession.codechef_url + '/session/limit':
html_page = lxml.html.fromstring(response.text)
all_inputs = html_page.xpath(r'//form//input')
payload = {i.attrib["name"]: i.attrib["value"] for i in all_inputs[::-1]}
response = self.codechef_session.post(CodechefSession.codechef_url + '/session/limit', data=payload)
soup = bs(response.content, 'lxml')
name = soup.find(text=username)
self.logged_in = bool(name)
if self.logged_in: self.username = username
return self.logged_in
评论列表
文章目录