Answer the question
In order to leave comments, you need to log in
How to login to a website using python requests?
Good day
There is a task to parse the site https://www.strava.com, we use Python for parsing.
It is necessary to parse the internal user data of the site. To do this, you need a login, and this is a problem. No matter what we try, the result is negative. Need expert help on this issue.
Thanks in advance
from bs4 import BeautifulSoup
import requests
from fake_useragent import UserAgent
from time import sleep
def authorize():
headers = {
'User-Agent': UserAgent().chrome,
}
# utf8: ✓
# 'utf8': '✓',
login_data = {
'utf8': '✓',
'plan': '',
'email': '[email protected]',
'password': 'pass',
}
with requests.Session() as s:
s.get('https://www.strava.com')
s.verify = False
url = 'https://www.strava.com/login/'
r = s.get(url)
soup = BeautifulSoup(r.text, 'html5lib')
login_data['authenticity_token'] = str(soup.find('input', attrs={'name': 'authenticity_token', 'type': "hidden"})['value'])
headers['X-CSRF-Token'] = soup.select_one('meta[name="csrf-token"]')['content']
headers['cookie'] = '; '.join([x.name + '=' + x.value for x in s.cookies])
sleep(1)
r2 = s.post(url, data=login_data, headers=headers)
r3 = s.get('https://www.strava.com/clubs/225082/members')
sleep(1)
print(r3.text)
def main():
authorize()
if __name__ == '__main__':
main()
Answer the question
In order to leave comments, you need to log in
Yes, here is business for 2 minutes:
import requests
from bs4 import BeautifulSoup
import time
headers = {'user-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:72.0) Gecko/20100101 Firefox/72.0'}
data = {'authenticity_token':'',
'email':'', # Email
'password':'' # Пароль
}
url = 'https://www.strava.com/session'
session = requests.Session() # Сессия
def get_token():# Метод, для получения токена
response = session.post(url,headers=headers)
soup = BeautifulSoup(response.text,"html.parser")
token = soup.find('input',{'name':'authenticity_token'}).get('value')
return token # Возвращает токен
def auth(): # Метод, для авторизации
response = session.post(url,headers=headers,data=data)
return response.text
data['authenticity_token'] = get_token() # Вызывает метод для получения токена, и результат заносим в словарь
time.sleep(2) # Пауза 2 сек :)
html = auth() # Авторизируемся. В html будет наш ответ после авторизации
if 'Log Out' in html: # Если строка 'Log Out' есть в html, значит авторизация прошла успешно
print('Login OK!')
else:
print('Login Error!')
Try to monitor the login to your account via Network (press f12 and select Network), log in and submit the form. In the Network tab, login data should have been sent to the server, look at what is being transmitted, most likely some token is being transmitted along with the password and name, and because of this, authorization cannot be performed. For authorization, you need to transfer all the data that should be transferred, and not just the username and password.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question