Question:
I have the following code for authorization. But it gives me the same html twice, but I need to first give out the authorization page, and then the one I am requesting. How can I resolve this? It's all about cookies, as I understand it.
import requests
from bs4 import BeautifulSoup
EMAIL = '***'
PASSWORD = '***'
def login(url):
values = {'email': EMAIL,
'password': PASSWORD
}
page = requests.post(url, data=values)
print(page.content)
def get_html(url):
responce = requests.get(url)
return responce.content
def parse(html):
soup = BeautifulSoup(html)
def main():
print(login('http://***.**.***.**'))
if login('http://***.**.***.**'):
print(get_html('http://***.**.***.**'))
if __name__ == "__main__":
main()
Answer:
First, as the comments suggest, create a session object:
def main():
session = requests.Session()
Pass it on:
if login('http://...', session):
In the login function, you need to check whether the authorization was successful. You can answer code:
def login(url, session):
values = {'email': EMAIL,
'password': PASSWORD
}
page = session.post(url, data=values)
print(page.content)
return page.status_code == 200
And in main we will add else:
if login('http://...'):
print(get_html('http://...', session))
else:
print('authorization failed')
Because most likely the problem is her. And how to make a normal authorization – you need to look at how it works on a specific site