How to maintain a login session during login using Python Script
I want to login to Ideone.com using a python script and then fetch stuff from my own account using subsequent requests using a python script. This is what I used to login to the site:
import requests import urllib from bs4 import BeautifulSoup url='http://ideone.com/account/login/' body = {'username':'USERNAME', 'password':'PASSWORD'} s = requests.Session() loginPage = s.get(url) soup = BeautifulSoup(loginPage.text) r = s.post(soup.form['action'], data = body) print r
This code successfully logs me into my ideone account. But if I make a subsequent call (using BeautifulSoup) to access my account details, it sends me the HTML login page again.
How can I save a session for a specific script so that it accepts subsequent calls? Thanks in advance and sorry if this was asked earlier.
+3
source to share
1 answer
Here's how we can do it:
from requests import session
from bs4 import BeautifulSoup
payload = {
'action' : 'login',
'username' : 'USERNAME',
'password' : 'PASSWORD'
}
login_url='http://ideone.com/account/login/'
with session() as c:
c.post(login_url, data = payload)
request = c.get('http://ideone.com/myrecent')
print request.headers
print request.text
0
source to share